This is a simple node.js server. Here's how it was created from scratch
- mkdir simple_server
- cd simple_server
- npm_init
- fill out the package.json info. use main.js as entry point
- npm i http-status-codes -S installs needed node_modules
- make a main.js file
- copy the following code into main.js
const port = 3000,
http = require( 'http' ),
httpStatus = require( 'http-status-codes' ),
app = http.createServer( ( request, response ) => {
console.log( 'Received an incoming request!' );
response.writeHead( httpStatus.OK, {
'Content-Type': 'text/html'
} );
let responseMessage = '<h1>Hello, Universe!</h1>';
response.write( responseMessage );
response.end();
console.log( `Sent a response : ${responseMessage}` );
} );
app.listen( port );
console.log( `The server has started and is listening on port number: ${port}` );
- add a .gitignore file in main directory
- add node_modules/ to .gitignore
- in terminal, run 'node main' to started
- open browser to localhost:3000
- should see "Hello, Universe"