【发布时间】:2021-01-08 22:34:54
【问题描述】:
我遵循了这个关于在 a2hosting 上设置 node.js 的精彩教程:https://www.youtube.com/watch?v=BAD5JyOymRg
除了 CSS 没有在 Firefox 或 Chrome 中加载并且由于某种原因引发 503 错误之外,一切似乎都正常。如果我将href 指向根本没有文件,它也会引发 503 错误。我尝试将 href 链接更改为 /stylesheets/style.css 或移动 style.css 但没有任何运气。
任何帮助将不胜感激!
server.js:
var express = require('express');
var app = express();
var path = require('path');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use('/testapp/static', express.static('public'));
app.get('/testapp', (req, res) => {
res.render('index', { title: 'Express' });
});
app.listen();
package.json:
{
"name": "app",
"version": "1.0.0",
"description": "My App",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"express": "^6.14.7",
"ejs": "~2.6.1"
},
"author": "",
"license": "ISC"
}
index.ejs:
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='http://*mysite*.com/testapp/static/stylesheets/style.css' />
</head>
<body>
<h1>Hello <%=title%></h1>
</body>
</html>
style.css:
body {
background-color: red;
}
这是文件夹结构:
testapp
-public
--stylesheets
---style.css
-views
--index.ejs
-server.js
-package.json
-node_modules
【问题讨论】:
-
您的网站是否驻留在代理(如 nginx)后面?
-
据我所知,它是使用 a2hosting 的,我还没有做任何花哨的事情
-
你能分享来自 503 的 http 响应头吗? 503 响应正文是空的吗?您是否在控制面板主页可访问的 a2hosting 错误日志中看到任何消息?
-
尝试使用绝对路径
app.use('/testapp/static', express.static(path.join(__dirname, 'public'))),如果可行,请告诉我们
标签: css node.js express hosting