json - nodejs res.json在html中显示

标签 json node.js mongodb

尝试将通过nodejs从mongo db查询的数据显示到html index.html。 脚本的作用是什么?它打开服务器连接,连接到 mongodb 并使用 datapicker 从网络表单显示结果查询,通过控制台我可以看到结果并且它工作正常,现在我需要将数据显示到网络。

目前还没有结果。有什么建议吗?

var express = require("express");
var app = express();
var router = express.Router();
var path = __dirname + '/views/';
var fs = require("fs");


const util = require('util')

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost/klevin';


router.use(function (req,res,next) {
    console.log("/" + req.method);
    next();
});



router.get("/",function(req,res){

    res.sendFile(path + "index.html");



    var data_e_fillimit = req.param('start_time');
    //console.log(params.startDate)
    console.log('Data e fillimit '+data_e_fillimit)


    var data_e_mbarimit= req.param('endtime_time');
    //console.log(params.startDate)
    console.log('Data e mbarimit '+data_e_mbarimit)


// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {

  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to', url);


    // Get the documents collection
    var collection = db.collection('frames');

    //We have a cursor now with our find criteria
    var cursor = collection.find({
      tv: 'tematv', 
      date_created: {"$gte": new Date(data_e_fillimit) , "$lte": new Date(data_e_mbarimit) }});

    //We need to sort by age descending
    cursor.sort({_id: -1});

    //Limit to max 10 records
    cursor.limit(50);

    //Skip specified records. 0 for skipping 0 records.
    cursor.skip(0);


    //Lets iterate on the result
    cursor.each(function (err, doc) {

      if (err) {

        console.log(err);
        //res.json(err);

      } else {

        console.log('Fetched:', doc);
       // res.json({ user: 'tobi' })



      }
    });


  }



});



});


/*router.get("/about",function(req,res){
    res.sendFile(path + "about.html");
});


router.get("/contact",function(req,res){
    res.sendFile(path + "contact.html");
});*/

app.use("/",router);

/*app.use("*",function(req,res){
  res.sendFile(path + "404.html");
});*/

app.listen(3000,function(){
    console.log("Live at Port 3000");
});

最佳答案

使用ejs(npm install ejs --save)包尝试如下:

app.engine('html', require('ejs').renderFile);    
app.set('view engine', 'ejs');

app.get('/', function (req, res){
    res.render('index.html',{
      foo:bar
    });
});

现在可以使用传递给index.html的这个对象

关于json - nodejs res.json在html中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41694181/

相关文章:

javascript - 如何将JSON格式的日期字符串解析成日期格式

c# - C# 和 typescript 之间 JSON 的端到端类型安全

javascript - 将只有键的对象分配给 require 语句

javascript - 如何在单个存储库中与其他团队共享本地 npm 包?

java - MongoTemplate如何在查询中添加两个以上 "orOperator"

php - 如何在 PHP 中将字符串转换为 JSON 对象

java - 具有 JPA 实体的 JAX-RS JSON 序列化循环

使用 Node JS 转换 Java 到 JavaScript

python - 检索 GridFS 中要从 Flask 发送的文件?

javascript - 按相关性能问题排序