1/* ====== create node.js server with core 'http' module ====== */
2// dependencies
3const http = require("http");
4
5// PORT
6const PORT = 3000;
7
8// server create
9const server = http.createServer((req, res) => {
10 if (req.url === "/") {
11 res.write("This is home page.");
12 res.end();
13 } else if (req.url === "/about" && req.method === "GET") {
14 res.write("This is about page.");
15 res.end();
16 } else {
17 res.write("Not Found!");
18 res.end();
19 }
20});
21
22// server listen port
23server.listen(PORT);
24
25console.log(`Server is running on PORT: ${PORT}`);
26
27/* ========== *** ========== */
28
29/* ====== create node.js server with express.js framework ====== */
30// dependencies
31const express = require("express");
32
33const app = express();
34
35app.get("/", (req, res) => {
36 res.send("This is home page.");
37});
38
39app.post("/", (req, res) => {
40 res.send("This is home page with post request.");
41});
42
43// PORT
44const PORT = 3000;
45
46app.listen(PORT, () => {
47 console.log(`Server is running on PORT: ${PORT}`);
48});
49
50
51// ======== Instructions ========
52// save this as index.js
53// you have to download and install node.js on your machine
54// open terminal or command prompt
55// type node index.js
56// find your server at http://localhost:3000
1const http = require('http');
2
3const hostname = '127.0.0.1';
4const port = 3000;
5
6const server = http.createServer((req, res) => {
7 res.statusCode = 200;
8 res.setHeader('Content-Type', 'text/plain');
9 res.end('Hello World');
10});
11
12server.listen(port, hostname, () => {
13 console.log(`Server running at http://${hostname}:${port}/`);
14});
1const express = require('express');
2const server = express();
3
4const PORT = 3000;
5
6// Homme page
7server.get('/', (req, res) => {
8 return res.send("<h1 style='text-align: center;'>Hello,<br />from the Express.js server!</h1>");
9})
10
11// About page
12server.get('/about', (req, res) => {
13 return res.send('<h2 style="text-align:center">About us</h2>');
14})
15
16// 404 page
17server.use((req, res, next) =>{
18 res.status(404);
19
20 // respond with html page
21 if (req.accepts('html')) {
22 res.sendFile(__dirname + '/error404.html');
23 return;
24 }
25 // respond with json
26 else if (req.accepts('json')){
27 res.send({
28 status: 404,
29 error: 'Not found'
30 });
31 return;
32 }
33 // respond with text
34 else {
35 res.type('txt').send('Error 404 - Not found');
36 }
37});
38
39server.listen(PORT, () => {
40 console.log(`Server is running on port ${PORT}`);
41});
42
1// content of index.js
2const http = require('http')
3const port = 3000
4
5const requestHandler = (request, response) => {
6 console.log(request.url)
7 response.end('Hello Node.js Server!')
8}
9
10const server = http.createServer(requestHandler)
11
12server.listen(port, (err) => {
13 if (err) {
14 return console.log('something bad happened', err)
15 }
16
17 console.log(`server is listening on ${port}`)
18})
19
1// code by VARSHITH REDDY SATTI
2// to create a server in node.js you should.
3var http = require('http');
4http.createServer(function (req, res) {
5 res.writeHead(200, {'Content-Type': 'text/html'});
6 res.write("write html code to display you test")
7 res.end();
8}).listen(8080);
9// save this as httpServer.js
10// run this by typing node httpServer.js in the command line
11// to acess your server got to http://localhost:8080
1import express from 'express';
2const server = express();
3
4const port = 8080;
5
6server.get('/', (req, res) => {
7 return res.send('Hello, Express.js!');
8})
9
10server.listen(port, () => {
11 console.log(`Server is running on port ${port}`);
12});