KEMBAR78
Serving Static Files With The HTTP Module | PDF | Web Server | Internet & Web
0% found this document useful (0 votes)
9 views8 pages

Serving Static Files With The HTTP Module

Uploaded by

Yokesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Serving Static Files With The HTTP Module

Uploaded by

Yokesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Serving Static files with the HTTP Module

1. For creating server on Node JS we have used “http” module. For using
any module in Node JS we have to use “require” module. So fist we import the
“http” module.

1. var http=require("http");
2. Now we are creating a server. For creating the server we have to use
‘createServer ‘method of http module and this method takes 2
parametersing [request and response] as show below.
1. var server = http.createServer(function(request, response) {});
3. After that we have to set the content type as plain text for sending response
to the client. As shown in
1. var server = http.createServer(function(request, response) {
2. response.writeHead(200, {
3. 'Content-Type': 'text/plain'
4. });
5. });
Here you can set any response type like plain text or html etc.
4. Now for sending response to client we have to use response.write() method.
And finally you have to call response.end() method for ending the response.
1. var server = http.createServer(function(request, response) {
2. response.writeHead(200, {
3. 'Content-Type': 'text/plain'
4. });
5. response.write("This is Test Message.");
6. response.end()
; 7. });
5. Now we have to start listening to this server; on any http post you can use
any port which is available (not used by any other application in your
computer.) Here I have taken 8082 port.

server.listen(8082);
sample.js

var http=require('http');

var fs =require('fs');

var server = http.createServer(function(request,response)


{ response.writeHead(200,{'Content-Type': 'text/html'});
fs.readFile('sample.html', null, function (error, data) {

if (error) {
response.writeHead(404)
;

respone.write('Whoops! File not found!');

} else {
response.write(data
);
}

response.end();

});

});

server.listen(8080);

sample.html

<html>

<body>

<p> Script example </p>

<script>
var p,txt;

p = prompt("Enter the value between 1 and 5 for


a!!","0"); if (p == 5){

txt = "Print 5!";

else {

txt = "Not 5";

document.write(txt);

</script>

<h1>Hello Welcome</h1>

</body>

</html>
Output:

Running the Javascript using node command:

Open any Browser and type localhost:8080


in URL:
Introduction to Express

What is Express?

 Express.js is a web framework for Node.js. It is a fast, robust and asynchronous in nature.
 It makes it easier to organize your application’s functionality with middle ware and
routing;
 it adds helpful utilities to Node.js’s HTTP objects;it facilitates the rendering of dynamic
HTTP objects.

Let's see some of the core features of Express framework:

o It can be used to design single-page, multi-page and hybrid web applications.


o It allows to setup middlewares to respond to HTTP Requests.
o It defines a routing table which is used to perform different actions based on HTTP
method and URL.
o It allows to dynamically render HTML Pages based on passing arguments to templates.

Why use Express


o Ultra fast I/O
o Asynchronous and single threaded
o MVC like structure
o Robust API makes routing easy

Installing Express on Windows (WINDOWS 10)

STEP-1: Creating a directory for our project and make that our working directory.
$ mkdir gfg
$ cd gfg
STEP-2: Using npm init command to create a package.json file for our project.
$ npm init
This command describes all the dependencies of our project. The file will be updated when
adding further dependencies during the development process, for example when you set up
your build system.

Keep pressing enter and enter “yes/no” accordingly at the terminus line.
STEP-3: Installing Express
Now in your gfg(name of your folder) folder type the following command line:
$ npm install express --save

NOTE- Here “WARN” indicates the fields that must be entered in STEP-2.
STEP-4: Verify that Express.js was installed on your Windows:
To check that express.js was installed on your system or not, you can run the following
command line on cmd:
C:\Users\Admin\gfg\node_modules>npm --version express
The version of express.js will be displayed on successful installation.

Example 1: Setting up a basic get request route on the root URL (‘/’ path) of the server.
1. With app.get() we are configuring our first route, it requires two arguments first one is the path
and, the second one is a function that will be executed when anyone requests this path with GET
method. The express provides the request and response object as a parameter to all such types of
functions.
2. The req is a giant object which will be received from the user and res is an object which will be
sent to the user after the function finishes execution.
3. Later we are calling status() method it takes an HTTP status code as an argument and when the
response is returned, the status will be sent along.
4. Finally, we are returning the response to the user. The send() method takes a string, object, array,
or buffer as an argument and is used to send the data object back to the client as an HTTP
response, also there are lots of types of response in express like res.json() which is used to send
JSON object, res.sendFile() which is used to send a file, etc.
app.js
const express = require('express');
const app = express();
const PORT = 3000;

app.get('/', (req, res)=>{


res.status(200);
res.send("Welcome to root URL of Server");
});

app.listen(PORT, (error) =>{


if(!error)
console.log("Server is Successfully Running,
and App is listening on port "+ PORT)
else
console.log("Error occurred, server can't start", error);
}
);
Step to run the application: Save this code, restart the server, and open the localhost on the
given port. When client request with the appropriate method on the specified path ex: GET
request on ‘/’ path, our function is returning the response as plain text If we open the network
section in chrome developers tools (press Ctrl+Shift+I to open) we will see the response returned
by the localhost along with all information.
Output:

Example 2: Setting up one more get request route on the ‘/hello’ path.

You might also like