2016-11-07 56 views
0

我有简单的server.js文件,如下所示:快速路由的单页的应用程序似乎并没有工作

const express = require('express'); 
const app = express(); 

app.set('port', (process.env.PORT || 3031)); 

if (process.env.NODE_ENV === 'production') { 
    app.use(express.static('build')); 
    app.get('*', (req, res) => { 
    res.sendFile('build/index.html'); 
    }) 
} 

app.listen(app.get('port'), (error) => { 
    if (error) return console.error(error.message); 
    console.log(`Server started at: http://localhost:${app.get('port')}/`); 
}) 

我希望它重新路由的所有路径,以在生产index.html,然后启动服务器。目前路由位似乎行不通,因为我收到以下错误:

Server started at: http://localhost:3031/ TypeError: path must be absolute or specify root to res.sendFile

+0

重复:https://stackoverflow.com/questions/25463423/res-sendfile-absolute-path –

回答

0

尝试添加根目录的路径

res.sendFile(__dirname + '/build/index.html'); 
0

res.sendFile需要一个绝对路径,就像错误说。

尝试使用__dirname global:中

res.sendFile(__dirname + 'build/index.html');