[英]Node (Express) doesn't start
I was trying out Express and trying to learn it, and I tried to start a server, but nothing's happening.我正在尝试 Express 并尝试学习它,我尝试启动一个服务器,但什么也没发生。 I'm trying to access 127.0.0.1:8080 through Express, and I copied ALMOST all of the example (but not all).
我正在尝试通过 Express 访问 127.0.0.1:8080,并且我复制了几乎所有示例(但不是全部)。 This is my code:
这是我的代码:
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => function() {
let ip = req.ip
console.log(ip);
res.send('Hello World')
})
app.listen(port, ()=>console.log(`Server started on port ${port}`))
The app will log after starting node server.js
, but nothing will appear at 127.0.0.1:8080.该应用程序将在启动
node server.js
后登录,但在 127.0.0.1:8080 处不会出现任何内容。 Is there something wrong with my code?我的代码有问题吗?
You have an error using arrow functions, try the following:使用箭头函数时出错,请尝试以下操作:
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => {
let ip = req.ip
console.log(ip);
res.send('Hello World')
})
app.listen(port, ()=>console.log(`Server started on port ${port}`))
You have made a mistake in your endpoint callback function.您在端点回调函数中犯了一个错误。
function
keyword is not used when arrow function syntax is used.使用箭头函数语法时不使用
function
关键字。
Either you use function() {}
, or you use () => {}
to write functions.要么使用
function() {}
,要么使用() => {}
来编写函数。
Correct code snippet would be:正确的代码片段是:
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => {
let ip = req.ip
console.log(ip);
res.send('Hello World')
})
app.listen(port, ()=>console.log(`Server started on port ${port}`))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.