简体   繁体   中英

Trouble getting my node.js server to listen with

I am learning node.js and I am trying to figure out how I can get my program to listen at a specific port, but I just keep getting error messages. Is there any easier way I could be doing this, or what do I need to change in my code to allow this to work?

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}')
}

I think you should try something like the following to get started.

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

Or in your case, I think that the following changes will work:

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}')
})

It seems you had a syntax error as well as a typo in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM