Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to create a server in node js

// code by VARSHITH REDDY SATTI
// to create a server in node.js you should.
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write("write html code to display you test")
  res.end();
}).listen(8080);
// save this as httpServer.js
// run this by typing node httpServer.js in the command line
// to acess your server got to http://localhost:8080
Comment

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

how to start a node server

// app.js

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

// 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/html'});
    fs.createReadStream('index.html').pipe(res)
});

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

how to start node server

// 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!
');
});

// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');
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 :: check if the document is ready js 
Javascript :: concantene number in js 
Javascript :: vue cli - Uncaught SyntaxError: Unexpected token < 
Javascript :: dictionary in javascript 
Javascript :: js reduce break 
Javascript :: check checkbox based on value using jquery 
Javascript :: javascript detect if object is date 
Javascript :: vite.config.js 
Javascript :: export apk react native 
Javascript :: nodejs csv to json from link 
Javascript :: flatlist horizontal 
Javascript :: route component with props 
Javascript :: regexp object javascript 
Javascript :: js array return only certain positions 
Javascript :: set image size phaser 
Javascript :: Object.hasOwnProperty.call 
Javascript :: js string to array 
Javascript :: index of value in array 
Javascript :: how to read json file in python stack overflow 
Javascript :: picker change event react native 
Javascript :: how to draw ellipse in javascript canvas 
Javascript :: regular expression for thousand separator 
Javascript :: strapi change user password 
Javascript :: remove double quotes from json array javascript 
Javascript :: js get value of input 
Javascript :: days difference in moment js 
Javascript :: jquery append div 
Javascript :: number object js 
Javascript :: js date enlever jour 
Javascript :: for of loop javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =