xxxxxxxxxx
## Make sure you run this command in the app directory.
node .
xxxxxxxxxx
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}`))
xxxxxxxxxx
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}`);
});
xxxxxxxxxx
//HTTP MODULE NODE.JS
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
});
server.listen(5000);
xxxxxxxxxx
// 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!\n');
});
// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');
xxxxxxxxxx
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}/`);
});
xxxxxxxxxx
// Requiring the module
const http = require('http');
// Creating server object
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>GeeksforGeeks</title><head>');
res.write('<body><h2>Hello from Node.js server!!</h2></body>');
res.write('</html>');
res.end();
});
// Server setup
server.listen(3000, ()=> {
console.log("Server listening on port 3000")
});
xxxxxxxxxx
// Import the http module
const http = require('http');
// Define the server hostname and port
const hostname = 'localhost';
const port = 3000;
// Create a server object
const server = http.createServer((req, res) => {
res.statusCode = 200; // Set the response status code
res.setHeader('Content-Type', 'text/plain'); // Set the content type of the response
// Send a response back to the client
res.end('Hello, World!\n');
});
// Start the server
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});