在express中处理静态文件

问题描述 投票:0回答:2

我有这个文件结构:

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

如何修复错误?

node.js express
2个回答
1
投票

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

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

希望有帮助。


0
投票

跨平台版本,将路径分隔符带入帐户:

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

© www.soinside.com 2019 - 2024. All rights reserved.