express.static没有指向正确的道路

时间:2017-04-19 18:35:49

标签: node.js

每次尝试到达/about

时,我都会遇到问题

此代码有效且一切正常

var express = require('express');  
var path = require('path');

var app = express();                 // define our app using express

var port = process.env.PORT || 3000;        // set our port

console.log(path.join(__dirname, 'public'));

app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname, 'public')+'/index.html');   
 })
.get('/about', function(req, res) {
    console.log(__dirname);
     res.sendFile(path.join(__dirname, 'public')+'/about.html');   
 })
.get('/signIn', function(req, res) {
    res.sendFile(path.join(__dirname, 'public')+'/signIn.html');   
});

app.listen(port);
console.log('Magic happens on port ' + port);

但是当我尝试将公共文件夹放在express.static中时出现错误

  

“错误:ENOENT:没有这样的文件或目录,stat'C:\ about.html'       在错误(本机)“

var port = process.env.PORT || 3000;        // set our port
app.use(express.static(path.join(__dirname, 'public')));
console.log(path.join(__dirname, 'public'));

app.get('/', function(req, res) {
    res.sendFile('/index.html');   
})
.get('/about', function(req, res) {
    console.log(__dirname);
    res.sendFile('/about.html');   
})
.get('/signIn', function(req, res) {
    res.sendFile('/signIn.html');   
});


app.listen(port);
console.log('Magic happens on port ' + port);

0 个答案:

没有答案