Andrew White
Andrew White

Reputation: 59

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

Upvotes: 0

Views: 35

Answers (1)

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

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.

Upvotes: 2

Related Questions