全局变量__dirname
查看api文档 https://nodejs.org/api/modules.html#modules_dirname
看到解释如下:
The directory name of the current module. This is the same as the path.dirname() of the __filename。
啥意思呢,及时返回nodejs 的js文件的所在目录。
有了这个变量之后,我们就可以用如下代码解决这个问题。
app.use(express.static(__dirname + '/public'));