Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

serving html file using 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

PREVIOUS NEXT
Code Example
Javascript :: how to numbers by checked in checkbox in javascript 
Javascript :: jquery check if class exists 
Javascript :: dangerouslySetInnerHTML 
Javascript :: firestore add document 
Javascript :: javascript early break reduce() method 
Javascript :: google maps js remove google maps logo 
Javascript :: javascript check if variable is object 
Javascript :: ternary operator angular template 
Javascript :: how to install yup in react native 
Javascript :: mongodb connection string node example localhost 
Javascript :: get dir from filepath javascript 
Javascript :: route pass props to component 
Javascript :: regexp constructor javascript 
Javascript :: jquery make new variable with dynamic name 
Javascript :: string-mask javascript 
Javascript :: font awesome react native icons 
Javascript :: javascript if a variable is undefined 
Javascript :: create multiple collections in mongodb 
Javascript :: javascript reduce fraction 
Javascript :: react refresh 404 error 
Javascript :: fetch post headers 
Javascript :: js change value of every value in an object 
Javascript :: SEQUELIZE OR 
Javascript :: find in array and change 
Javascript :: jquery add table row 
Javascript :: create function replace all n javescript 
Javascript :: jquery add div 
Javascript :: regex find string between two characters 
Javascript :: jquery open page in new tab 
Javascript :: bs modal service close 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =