npm run serve
时,一切正常。应用程序在localhost:8080上运行,就像我预期的那样。但是,当我为该应用程序创建后端(使用Node.js和Express的新项目)时,我启动了简单服务器,并且在终端中看起来很好,但是当我想在浏览器中转到localhost:5000查看基本的Hello World时,页面会加载一段时间,然后显示网页未发送任何数据。
这是我的app.ts:
import express, { Application, Request, Response, NextFunction } from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import 'reflect-metadata'
const app: Application = express()
const port = 5000
app.use(cors)
app.use(bodyParser.json({ limit: '20480kb' }))
app.get('/', (req, res) => {
res.send('Hello!')
})
const server = app.listen(port, 'localhost', () => console.log(`The server is running on port ${port}.`))
这是我的package.json scripts代码(将typescript编译为javascript正常运行):
"scripts": {
"start": "nodemon dist/app.js",
"build": "rm -rf dist/ && tsc -w",
"commit": "npx git-cz"
}
这是我的后端终端,一切正常运作:
https://istack.dev59.com/pJIb9.webp
我尝试使用netstat查看连接情况:
https://istack.dev59.com/n5naM.webp
我的问题是无法在Web浏览器上访问服务器,请帮忙。