Express 路由不适用于 Heroku 部署
Express routes not working with Heroku deployment
我正在尝试将 client/server 应用程序部署到 heroku,使用节点作为服务器,通过 express 为 index.html 提供服务。
部署到 heroku 后,Express 未正确路由服务器以找到 index.html。
我在查看已部署的项目时收到“404:找不到网页”。
项目结构:
public
-- img
-- index.html
-- index.js
服务器
-- server.js
server.js
const express = require('express');
const app = express();
const path = require('path')
app.use(express.static(path.join(__dirname, '..', 'public')))
我什至在控制台记录了我在 server.js 中尝试使用的路径,我可以看到路径输入正确。例如:
const fs = require('fs')
fs.readdirSync(path.join(__dirname , '..', 'public')).forEach(file => {
console.log(file);
});
在我的服务器日志中产生输出:
img
index.html
index.js
然而,当我连接到已部署的服务器时,出现以下日志:
heroku[路由器]: at=info method=GET path="/"
表示它仍在路由到 / 而不是 express 告诉它的地方 (/public)
我是不是在 express 中遗漏了一些重要的行?
找到解决方案。问题是我试图将 express 和 node 与 socket.io 服务器一起使用,但没有正确设置它。
第一步是将 socket.io 从 v2.3 更新到 v4.4:
/**
* package.json
*/
// STEP 1
"dependencies": {
"express": "^4.17.2",
"socket.io": "^4.4.0",
...
}
第二步是使用 socket.io 的帐户正确设置服务器和路由:
/**
* server.js
*/
// STEP 2
const PORT = process.env.PORT || 3000
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
// this 'io' object is used to listen for client web-sockets, and links the
// server to a socket.io instance
const io = new Server(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"]
}
})
// 'cors' is just whitelisting the client to get info from the server
// in prod, instead of localhost, it would be the deployed prod website url
第三步也是最后一步是将服务器设置为侦听客户端交互:
/**
* server.js
*/
// STEP 3
server.listen(PORT, () => {
console.log(`Starting server on port ${PORT}`)
})
以下更改修复了服务器。请注意,在客户端,我必须使用 socket.io 提供的套接字客户端库来连接到服务器。之后一切正常。
我正在尝试将 client/server 应用程序部署到 heroku,使用节点作为服务器,通过 express 为 index.html 提供服务。
部署到 heroku 后,Express 未正确路由服务器以找到 index.html。
我在查看已部署的项目时收到“404:找不到网页”。
项目结构:
public
-- img
-- index.html
-- index.js
服务器
-- server.js
server.js
const express = require('express');
const app = express();
const path = require('path')
app.use(express.static(path.join(__dirname, '..', 'public')))
我什至在控制台记录了我在 server.js 中尝试使用的路径,我可以看到路径输入正确。例如:
const fs = require('fs')
fs.readdirSync(path.join(__dirname , '..', 'public')).forEach(file => {
console.log(file);
});
在我的服务器日志中产生输出:
img
index.html
index.js
然而,当我连接到已部署的服务器时,出现以下日志:
heroku[路由器]: at=info method=GET path="/"
表示它仍在路由到 / 而不是 express 告诉它的地方 (/public)
我是不是在 express 中遗漏了一些重要的行?
找到解决方案。问题是我试图将 express 和 node 与 socket.io 服务器一起使用,但没有正确设置它。
第一步是将 socket.io 从 v2.3 更新到 v4.4:
/**
* package.json
*/
// STEP 1
"dependencies": {
"express": "^4.17.2",
"socket.io": "^4.4.0",
...
}
第二步是使用 socket.io 的帐户正确设置服务器和路由:
/**
* server.js
*/
// STEP 2
const PORT = process.env.PORT || 3000
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
// this 'io' object is used to listen for client web-sockets, and links the
// server to a socket.io instance
const io = new Server(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"]
}
})
// 'cors' is just whitelisting the client to get info from the server
// in prod, instead of localhost, it would be the deployed prod website url
第三步也是最后一步是将服务器设置为侦听客户端交互:
/**
* server.js
*/
// STEP 3
server.listen(PORT, () => {
console.log(`Starting server on port ${PORT}`)
})
以下更改修复了服务器。请注意,在客户端,我必须使用 socket.io 提供的套接字客户端库来连接到服务器。之后一切正常。