// 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"
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}`))
var http = require('http');
http.createServer(function (req, res) {
res.write('Hello World!');
res.end();
}).listen(8080);
//HTTP MODULE NODE.JS
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
});
server.listen(5000);
import express from 'express';
const server = express();
const port = 8080;
server.get('/', (req, res) => {
return res.send('Hello, Express.js!');
})
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
## Make sure you run this command in the app directory.
node .
const http = require('node: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, programmer!');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});