表达js路由问题

时间:2014-11-28 19:58:05

标签: javascript node.js express

我希望在使用sendFile时调用我的index.html页面,但是遇到了问题。 问题是它实际上向我展示了index.html,尽管没有任何css和js \ jquery效果。

server.js:

var express = require("express");
var app = express();

var server = app.listen(8000, function () {

var host = server.address().address;
var port = server.address().port;

console.log('Example app listening at http://%s:%s', host, port)

});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html')

});

1 个答案:

答案 0 :(得分:1)

您需要在代码中添加此行

app.use(express.static(__dirname + '/public')); // or another directory, it depends on where your static files are located

这是用于提供给定目录there is very good article about this middleware

中的文件的中间件