【发布时间】: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