Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node server index.html

// Create server.js file:
const http = require("http");
const fs = require("fs"); // this is filesystem which allows to read and save external files

const server = http.createServer((req, res) => {
  
  // set header content type
  res.setHeader("Content-Type", "text/html");

  // send html file
  fs.readFile("./views/index.html", (err, data) => {
    if (err) {
      console.log(err);
    } else {
      res.end(data);
    }
  });
});

server.listen(3000, "localhost", () => {
  console.log("listening for requests on port 3000");
});

// In directory with server.js create folder "views" and inside create file "index.html":
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Full success</title>
</head>
<body>
    <h1>Ladies and Gentlemans</h1>
    <h2>We're in!</h2>
</body>
</html>

// Now, open directory with server.js in terminal and run:
node server.js

// You can see the results in browser at http://localhost:3000

// NOTE: If you want to handle different adresses/pages/routes like /about, /contact or /blog search in google/grepper for "node server with routes"
Comment

node server index.html

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

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

// Now, run your web server using node app.js. Visit http://localhost:3000 and you
will see a message saying "Hello World".
Comment

PREVIOUS NEXT
Code Example
Javascript :: audio element javascript 
Javascript :: react select error handle 
Javascript :: js json escape 
Javascript :: db.json code 
Javascript :: for each array 
Javascript :: Remove items from an index position 
Javascript :: vue nested loop 
Javascript :: how to count seconds in javascript 
Javascript :: nuxt get client windows size 
Javascript :: clone an object in javascript 
Javascript :: nodejs import readline 
Javascript :: how to sum the array values in javascript 
Javascript :: javascript unshift 
Javascript :: javascript less than but greater than 
Javascript :: NodeJS 10.24.1 
Javascript :: chnage classname of div 
Javascript :: highlight nav menu on scroll with javascript 
Javascript :: push javascript 
Javascript :: how to convert string to number in javascript 
Javascript :: react bootstrap navbar align right buttons 
Javascript :: age validation jquery 
Javascript :: components in react 
Javascript :: why geting empty array from mongodb 
Javascript :: extract data from object when it match with array of ids js 
Javascript :: check if property exists javascript 
Javascript :: Destructuring of object in ES6 
Javascript :: jsx example 
Javascript :: afficher une variable dans la console javascript 
Javascript :: java script hash 
Javascript :: js phone number validation 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =