Error starting a Javascript script in Nodejs

1

I have a small problem when starting a script in the javascript language whose library is nodejs. The version of the node that I have installed is v6.11.1 and my computer is a Mac whose operating system is MacOS Sierra. The script that I'm trying to start is a simple hello world, since I'm starting:

var http = require('http');

http.createServer(function (req, res) {

res.writehead(200, {'Content-Type': 'text/plain'});
res.end('hola #backendpro\n');

}).listen(3000, '127.0.0.1');

console.log('Server running at [http.://127.0.0.1:3000/'][1]);

I write to the cd console and drag my folder where I have the script and then I type node and the name of my file that I want to run and the console returns a Server running at http.://127.0.0.1:3000/

until I open the browser and at the URL I type localhost: 3000 and the following appears on the console:

  

Mac-mini-of-Ivan: node ivan $ node holanode.js Server running at   http.://127.0.0.1:3000//Users/ivan/Desktop/node/holanode.js:5   res.writehead (200, {'Content-Type': 'text / plain'});       ^

     

TypeError: res.writehead is not a function       at Server. (/Users/ivan/Desktop/node/holanode.js:5:5)       at emitTwo (events.js: 106: 13)       at Server.emit (events.js: 191: 7)       at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js: 546: 12)       at HTTPParser.parserOnHeadersComplete (_http_common.js: 99: 23) Mac-mini-of-Ivan: node ivan $ node holanode.js Server running at    link /Users/ivan/Desktop/node/holanode.js:5   res.writehead (200, {'Content-Type': 'text / plain'});       ^

     

TypeError: res.writehead is not a function       at Server. (/Users/ivan/Desktop/node/holanode.js:5:5)       at emitTwo (events.js: 106: 13)       at Server.emit (events.js: 191: 7)       at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js: 546: 12)       at HTTPParser.parserOnHeadersComplete (_http_common.js: 99: 23)

and I do not know how to solve it, I have read many articles but none have helped me at all. I would be very grateful to the one who solves this problem for me.

    
asked by Ivan Navas 27.07.2017 в 21:56
source

2 answers

1

The methods are "case sensitive", that is, you must use capital letters in the names. In this case, you have res.writehead but the name of the method requires a capital H.

Use res.writeHead instead of res.writehead .

    
answered by 27.07.2017 в 22:10
0

Your code is fine, but you can improve it a bit more using the new syntax of ES2015 and properly separating the server to perform operations on it and not enchain everything in a sentence:

const http = require('http');

const hostname = '127.0.0.1';
const port = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log('Server running at http://${hostname}:${port}/');
});

One thing to keep in mind when declaring the port is to use the environment variables 'env' of NodeJS, since when you deploy the server, you will allow it to choose the port set in the variable of environment and not a specific port, because if not, it will always be listening on port 3000 and at some point it may not be available or is not useful for the requirements of your ecosystem. It all depends on the execution environment of the server that you deploy.

    
answered by 28.07.2017 в 12:41