【问题标题】:NodeJS Middelware/Routing Data transferNode JS 中间件/路由数据传输
【发布时间】:2019-10-29 16:57:49
【问题描述】:

您好,我正在尝试向客户端传输更多数据。我在 NodeJS express 中使用了中间件的示例代码。

我想读取 2 个不同的文件并将数据传输到客户端。我已成功传输 1 个文件数据。如何添加多个?

我应该怎么做?,我试过'send'和'json'但是我看不到我的网站前端

var express = require('express');
var router = express.Router();
const fs = require('fs');

/* GET home page. */

// const myHtml = require('fs').readFileSync(<path to html>);
const myHtml = fs.readFileSync('./views/index.html', 'utf-8');

//Data from server to client, this works.
const myJson = fs.readFileSync("./apidata.json", 'utf-8');

//I want to add a second one here
const apisparkline = fs.readFileSync("./apisparkline.json", 'utf-8');


console.log("server is running");


router.get('/', function(req, res, next) {
  //This works perfect
  res.end(myHtml.replace(/jsonapidata/g, JSON.stringify(myJson, '', 2)));


  //how should I do this?, I have tried 'send' and 'json' but then I cant see my front end of the website
  res.end(myHtml.replace(/sparklinedata/g, JSON.stringify(apisparkline, '', 2)));
});

module.exports = router;

【问题讨论】:

    标签: javascript node.js routing middleware


    【解决方案1】:

    简单使用,

    res.end(myHtml.replace(/jsonapidata/g, JSON.stringify({myJson,apisparkline}, null, 2)));
    

    更好的方法,

    res.json({myJson,apisparkline})
    

    然后,在客户端格式化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2013-08-22
      相关资源
      最近更新 更多