Skip to content

Instantly share code, notes, and snippets.

@barmgeat
Last active June 23, 2019 14:08
Show Gist options
  • Save barmgeat/6fdd1cae5a771a6ac79aaffb3c8b6da0 to your computer and use it in GitHub Desktop.
Save barmgeat/6fdd1cae5a771a6ac79aaffb3c8b6da0 to your computer and use it in GitHub Desktop.
// we need this module to create the server and handel the request from the user
const http = require('http');
// here is the host where the server will work
const hostname = '127.0.0.1';
// you can use any port but the likely for node.js is 3000
const port = 3000;
// here is a call back function we will talk about it later but this function called when the user requset the server URL
const server = http.createServer((req, res) => {
// request status code 200 on connection success will get back tothe user
res.statusCode = 200;
// the header for the request
res.setHeader('Content-Type', 'text/plain');
// resposne to clinet
res.end('Hello, World!\n');
// log in the server
console.log('hello world')
});
// start the server function when we run node server in the shell or command prompt
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment