Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

basic server on node.js

const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 3000;
const home = fs.readFileSync('index.html')
const about = fs.readFileSync('./about.html')
const services = fs.readFileSync('./services.html')
const contact = fs.readFileSync('./contact.html')

const server = http.createServer((req, res)=>{
    console.log(req.url);
    url = req.url;

    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    if(url == '/'){
        res.end(home);
    }
    else if(url == '/about'){
        res.end(about);
    }
    else if(url == '/services'){
        res.end(services);
    }
    else if(url == '/contact'){
        res.end(contact);
    }
    else{
        res.statusCode = 404;
        res.end("<h1>404 not found</h1>");
    }
});

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

node js simple server

const http = require('http');

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Hello, World!');
}

const server = http.createServer(requestListener);
server.listen(8080);
Comment

simple nodejs server

// One Line ES6
require('http').createServer((req, res) => res.end(`You're in: ${req.url}`)).listen(8080, err => err ? console.log : console.log(`Listening on port 8080`))
Comment

PREVIOUS NEXT
Code Example
Javascript :: to 2 decimal places javascript 
Javascript :: nodejs stream 
Javascript :: react native refresh on pull down 
Javascript :: password regex javascript 
Javascript :: js regex 
Javascript :: pop-under window before current page 
Javascript :: yarn install python2 not found 
Javascript :: Material-ui Accessibility icon 
Javascript :: When JavaScript was invented month?* 
Javascript :: leaflet 
Javascript :: javascript recursive on object of arrays 
Javascript :: self-invoking function 
Javascript :: jest mock implementation once 
Javascript :: how to use yarn to create next app 
Javascript :: js how to filter range imutable array 
Javascript :: how to get keys in an object javascript 
Javascript :: jquery clone object 
Javascript :: how to protect routes in react router v6 
Javascript :: ejs 
Javascript :: react table className 
Javascript :: if isset handlebars js 
Javascript :: bitcoin prices in javascript 
Javascript :: javascript select2 sortable 
Javascript :: flatlist react native keyextractor 
Javascript :: node.js http server 
Javascript :: jquery elements which id doesnt contain string 
Javascript :: spotify uri 
Javascript :: smtp testing 
Javascript :: indexof js 
Javascript :: req.params 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =