serve npm code example

Example 1: serve build node npm

# in terminal cd to project root dir
# to build project:
npm run build
# to serve/host project locally:
serve -s ./build

Example 2: Create a server which serves at port 3000 and with node.js command prompt

// app.js

const http = require('http');

// Create an instance of the http server to handle HTTP requests
let app = http.createServer((req, res) => {
    // Set a response type of plain text for the response
    res.writeHead(200, {'Content-Type': 'text/plain'});

    // Send back a response and end the connection
    res.end('Hello World!\n');
});

// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');

//console
$ node app.js // runs it
//to open it in ur browser, go to http://localhost:3000.

Example 3: server npm

npm install --global http-server

Tags:

Misc Example