用express寻址静态文件



我有这样的文件结构:

app
-- app.js
client
-- views
-- source
---- js
------ main.js
---- css

我需要访问源中的静态文件,网址为/src。示例:localhost/src/js/main.js。我使用express static,并在app.js中这样写:

app.use(express.static('/src', path.join(__dirname, '../client/source')));

但服务器返回

var opts = Object.create(options || null)
                  ^
TypeError: Object prototype may only be an Object or null: /home/user/WebstormProjects/project/app/../client/source

如何修复错误?

我认为如果您使用的是ExpressJS 4.0。应该是:

app.use('/src', express.static(path.join(__dirname, '../client/source')));

希望能有所帮助。

最新更新