Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

http node

var fs  = require("fs")
var http  = require("http")

//in this example, I try to show an image
http.createServer((req, res) => {

    fs.readFile(`./images/${req.url}.jpg`, (err, data) => {
        if(err) {
            res.writeHead(404, {'Content-Type': 'text/plain'})
            res.end('Img not found')
        } else {
            res.writeHead(200, {'Content-Type': 'image/jpg'})
            res.end(data)
        }
    })
//change 3000 if you prefer
}).listen(1337, 'localhost')
Comment

nodejs http

const http = require('http')

const server = http.createServer((req, res) => {
  if (req.url === '/') { //Home Page
    res.end('Welcome to our home page')
  } else if (req.url === '/about') { //About page
    res.end('Here is our short history')
  } else {
    res.end(`
    <h1>Oops!</h1>
    <p>We can't seem to find the page you are looking for</p>
    <a href="/">back home</a>
    `) // Page not found
  }
})

server.listen(5000)
console.log('Server is listening on port 5000')
Comment

node http request

const http = require('http');

http.createServer((request, response) => {
  const { headers, method, url } = request;
  let body = [];
  request.on('error', (err) => {
    console.error(err);
  }).on('data', (chunk) => {
    body.push(chunk);
  }).on('end', () => {
    body = Buffer.concat(body).toString();
    // BEGINNING OF NEW STUFF

    response.on('error', (err) => {
      console.error(err);
    });

    response.statusCode = 200;
    response.setHeader('Content-Type', 'application/json');
    // Note: the 2 lines above could be replaced with this next one:
    // response.writeHead(200, {'Content-Type': 'application/json'})

    const responseBody = { headers, method, url, body };

    response.write(JSON.stringify(responseBody));
    response.end();
    // Note: the 2 lines above could be replaced with this next one:
    // response.end(JSON.stringify(responseBody))

    // END OF NEW STUFF
  });
}).listen(8080);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to disable and enable a button in jquery 
Javascript :: how to run react builed version 
Javascript :: remove element from array by name javascript 
Javascript :: drupal 9 get nid from node 
Javascript :: javascript create object key from variable 
Javascript :: how to check if the first letter of a string is capitalized or uppercase in js 
Javascript :: continue foreach javascript 
Javascript :: how to open print dialog box on button click 
Javascript :: why does my form reload the page? html js 
Javascript :: change property in array of objects javascript 
Javascript :: how to validate phone number regex javascript 
Javascript :: how to get sum array in javascript 
Javascript :: javascript remove some words list from string 
Javascript :: javascript default parameters 
Javascript :: regex email 
Javascript :: how to chunk a base 64 in javascript 
Javascript :: round down the number javascript 
Javascript :: AWS JavaScript SDK node 
Javascript :: apps script toast 
Javascript :: jquery wait for element to load 
Javascript :: angularjs onclick 
Javascript :: Quick Git Setup 
Javascript :: largest and smallest number in an array 1-100 javascript 
Javascript :: js - change div height on scroll 
Javascript :: js compare elements of two arrays 
Javascript :: angular input type text character limit 
Javascript :: use svg image in next js 
Javascript :: useLocation 
Javascript :: javascript array to string remove comma 
Javascript :: date format french js 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =