Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node server

// Create node.js server with routing. Create server.js file:
const http = require("http");

const server = http.createServer((req, res) => {
  // called every time request comes to the server (e.g. when you refresh page)
  console.log("request made: ", req.url); 
  
  // define what type of content you are sending (plain text or html or JSON file, etc.)
  res.setHeader("Content-Type", "text/html");

  // display response in browser
  res.write("<h1>Ladies and Gentlemans</h1>");
  res.write("<h2>We're in!</h2>");
  res.end();
});

server.listen(3000, "localhost", () => {
  console.log("listening for requests on port 3000"); // you can see this not in browser console, but in terminal
});

// Now, in terminal go to directory with server.js 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"

// NOTE: If you want to run external index.html file search in google/grepper for "node server index.html"
Comment

node js server

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Comment

PREVIOUS NEXT
Code Example
Javascript :: moment timezone set default timezone 
Javascript :: jquery get by name 
Javascript :: On pressing enter change the focus to the next input field 
Javascript :: object deep copy 
Javascript :: protected route in react js 
Javascript :: javascript string to lowercase 
Javascript :: how to remove duplicates in js array 
Javascript :: javascript format date 
Javascript :: set a value in session using javascript 
Javascript :: jquery fade out 
Javascript :: gatsby new 
Javascript :: axios react 
Javascript :: datatables search not working 
Javascript :: model validation 
Javascript :: jquery on wheel event 
Javascript :: $(document).ready(function() alert 
Javascript :: bootstrap datepicker mindate today 
Javascript :: jquery change tabs 
Javascript :: react native navigation navigate 
Javascript :: include other js files in a js file 
Javascript :: upload form with doc type in ajax 
Javascript :: javascript get current date format dd mm yyyy hh mm ss 
Javascript :: dynamic route vue 
Javascript :: server.js 
Javascript :: cordova android close app with back button 
Javascript :: mathjax new line 
Javascript :: javascript sum digits in string of numbers 
Javascript :: node print variable 
Javascript :: how to change root variable css 
Javascript :: js script 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =