尝试使用express为index.html视图提供静态资产(css,图像,js)。
在app.js中
app.use(express.static(path.join(__dirname, 'public')));
在index.html
<link rel="stylesheet" type="text/css" href="/css/style.css">
<script type="text/javascript" src="/js/app.js"></script>
<img src="/images/logo.jpg" alt="logo"/>
文件夹结构
public
css
style.css
images
logo.jpg
js
app.js
所有三个必需文件似乎都可以正常运行,因为它们的响应状态均为200,但是未呈现logo.jpg。
编辑:公用文件夹中的app.js不是用于启动服务器的脚本。
完整的app.js
require('dotenv').config();
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const logger = require('morgan');
const index = require('./routes/index');
const install = require('./routes/install');
const webhook = require('./routes/webhooks');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(logger('dev'));
app.use('/webhook', bodyParser.raw({ type: 'application/json' }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/install', install);
app.use('/webhook', webhook);
const server = http.createServer(app);
server.listen(3000, () => console.log('App running'));
完整文件夹结构
public
css
style.css
images
logo.jpg
js
app.js
routes
index
index.js
install
index.js
webhooks
index.js
views
app
index.html
error.html
app.js