我试图托管我的server.js文件,该文件在循环上配置了我的API,部署是成功的,但URL为任何端点调用返回状态500。我把React前端托管在github页面上。我不知道丢失了什么,也不知道为什么部署成功后它没有响应任何呼叫。是因为我没有一个终端配置为'/'吗?
server.js代码:
const express = require('express')
const cors = require('cors')
require("dotenv").config()
const {getDb, dbConnect} = require('./db')
const app = express()
app.use(cors({origin: 'https://mygithubpage.github.io/'}))
app.use(express.json())
let db
const port = process.env.PORT || 6900;
dbConnect((error) => {
if(!error) {
app.listen(port, () => {
console.log(`Now listening to port ${port}`)
})
db = getDb()
}
})
// Searching for email and password
app.post('/signIn', (req,res) => {
const user = req.body
db.collection('users')
.findOne({email: user.email, password: user.password})
.then(user => {
res.status(200).json(user)
})
.catch(error => {
res.status(500).json({error: 'user not found'})
})
})
循环部署日志:
2023-04-02T20:00:47.283Z: [CYCLIC] cloning...
2023-04-02T20:00:48.776Z: From https://github.com/mygithubpage/movie-app
* branch ddd079a5bf40fbb8e440632fcdd91b584265c9cb -> FETCH_HEAD
2023-04-02T20:00:49.984Z: HEAD is now at ddd079a added node in package.json
2023-04-02T20:00:49.998Z: [CYCLIC] Building...
2023-04-02T20:00:50.023Z: Build Configuration:
Root Path: /server
Output Path: /
Static Site: false
Runtime: nodejs18.x
Branch: main
Ref: ddd079a5bf40fbb8e440632fcdd91b584265c9cb
2023-04-02T20:00:50.238Z: [CYCLIC] verifying...
2023-04-02T20:00:50.308Z: [CYCLIC] using: node:v18.15.0 npm:10.1.0 runtime:nodejs18.x
[CYCLIC] building from: /server
2023-04-02T20:00:50.317Z: [CYCLIC] installing dependencies from: package-lock.json
2023-04-02T20:00:56.440Z:
added 110 packages in 6s
2023-04-02T20:00:56.456Z: [CYCLIC] running build if defined...
2023-04-02T20:00:56.950Z: [CYCLIC] pruning dev dependencies...
2023-04-02T20:00:57.746Z:
removed 2 packages in 353ms
2023-04-02T20:00:57.773Z: [CYCLIC] packaging 109.36 MB...
2023-04-02T20:00:57.775Z: [CYCLIC] bundling from ...
2023-04-02T20:01:02.914Z: [CYCLIC] done packaging
[CYCLIC] deploying...
2023-04-02T20:01:11.058Z: deployed ca-central-1 - 8.062s
2023-04-02T20:01:11.059Z: SUCCESS
took 24.1 seconds
api deployed at:
https://gleaming-plum-horse.cyclic.app
我尝试使用其他网站,如www.render.com,但每次超时后部署失败,并且启动服务'npm start'/'node server.js'的多个日志
当我向服务器发送GET/POST请求时,我得到这个错误
2023-04-03 13:11:35.635: grep: /var/task/package.json: No such file or directory
2023-04-03 13:11:35.665: grep: /var/task/package.json: No such file or directory
ERROR: Cannot find entry point.
2023-04-03 13:11:42.344:
ERROR: Application process finished with status code 0 before starting a server
Common causes/solutions for this include:
- Server listen method not called. Verify server is listening on a port: "app.listen(process.env.PORT||3000)"
- An error may have been caught without being logged. Verify try/catch blocks have appropriate logging.
server.js环境在本地运行正常。
我想你错过了app.listen()
方法。
https://expressjs.com/en/starter/hello-world.html
上面的链接应该会让你很好地了解你错过了什么,除非你在上面分享的代码片段被截断了。
const express = require('express');
const app = express();
app.get("/", (req, res) => {
res.status(200).json( { msg: "Helloaaa" });
});
app.listen(7000, () => {
console.log(`The app has started on PORT 7000`)
});
上面的代码片段也是一个示例,它可以指导您找出可能出错的地方。我们初始化GET API,然后打开应用程序以监听端口7000。
如果没有端口,应用程序将不监听任何端口,因此将没有响应。