无法让我的node.js服务器监听

时间:2018-11-12 23:22:35

标签: javascript node.js

我正在学习node.js,我试图弄清楚如何让我的程序在特定端口上侦听,但我一直在获取错误消息。有什么更简单的方法可以执行此操作,或者我需要在代码中进行哪些更改才能使其正常工作?

const http = require('http');
const port = 3000

const requestHandler = (request, response) => {
  console.log(request.url)
  response.end('server is listening!')
}

const server = http.creatServer(requestHandler)
server.listen(port, (err) => ) {

  console.log('server is listening on ${port}')
}

1 个答案:

答案 0 :(得分:2)

我认为您应该尝试以下类似的方法来开始使用

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World!');
  res.end();
}).listen(8080);

或者就您而言,我认为以下更改将起作用:

const http = require('http');
const port = 3000

const requestHandler = (request, response) => {
  console.log(request.url)
  response.end('server is listening!')
}

const server = http.createServer(requestHandler)
server.listen(port, (err) =>  {

console.log('server is listening on ${port}')
})

似乎您的代码中有语法错误和错字。