ReferenceError:路径未定义:Express

IT技术 node.js reactjs express
2021-04-29 06:55:06

该页面已呈现,但当我按下任何链接时出现错误。

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);
});

提前致谢。

2个回答

在文件的头部,只需添加

const path = require('path');

你需要路径module吗

const path = require('path')