我正在尝试在nodejs 中加载HTML 文件。在公共(public)文件夹中,我有两个html文件test.html和index.html。我想使用nodejs来展示页面,但它适用于index.html并且对于测试的情况,它显示一个错误,指出res.sendFile不是一个函数。
var express = require('express');
var app = express();
var path = require('path');
// viewed at http://localhost:8080
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
// viewed at http://localhost:8080
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname + '/public/test.html'));
});
app.listen(8080);
最佳答案
要提供 JS、CSS、图像、html 文件等静态文件,您可以使用 Express 中的 express.static 内置中间件功能。 Serving static files in Express
假设您的“公共(public)”文件夹中是否有一些客户端所需的 JS、html 文件。然后你需要这样做:-
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
var server = app.listen(8080);
关于javascript - 在 Nodejs 中加载 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39193698/