简体   繁体   English

表达js路由问题

[英]express js routing issue

I wish to call my index.html page while using sendFile , but I ran into a problem. 我希望在使用sendFile调用index.html页面,但是遇到了问题。 The problem is that it actually shows me the index.html although without any css and js\\jquery effects. 问题是它实际上显示了index.html,尽管没有任何CSS和js \\ jquery效果。

server.js: 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')

});

You need add this line to your code 您需要将此行添加到代码中

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

this is middleware to serve files from given directory, there is very good article about this middleware 这是用于从给定目录提供文件的中间件, 关于该中间件的文章非常好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM