页面呈现,但当我按下任何链接时,它会出现错误。
ReferenceError: path is not defined
at app.get (/var/www/example.com/example-domain/server.js:106:19)
at Layer.handle [as handle_request] (/var/www/example.com/example-domain/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/example.com/example-domain/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/example.com/example-domain/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/example.com/example-domain/node_modules/express/lib/router/layer.js:95:5)
at /var/www/example.com/example-domain/node_modules/express/lib/router/index.js:281:22
at param (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:354:14)
at param (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:410:3)
at next (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:275:10)
下面是server.js中的代码:
app.use(express.static('../build'));
app.get('*', (req, res)=> {
const index = path.join(__dirname, '/', '../build', 'index.html' );
res.sendFile(index);
});
先谢了。
4条答案
按热度按时间8fsztsew1#
在你的文件头,只要加上
xzabzqsa2#
你需要路径模块吗
pwuypxnk3#
您需要在文件中包含Path模块,在应用程序中包含Path模块的语法:
0pizxfdo4#
将这段代码写在代码头中,这行代码导入内置的Node.js path模块,该模块提供了处理文件和目录路径的实用程序。
通过要求使用path模块,您可以使用它的方法以安全和跨平台兼容的方式操作文件路径。