简体   繁体   中英

express js routing issue

I wish to call my index.html page while using sendFile , but I ran into a problem. The problem is that it actually shows me the index.html although without any css and js\\jquery effects.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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