How to serve static file in express js

Leave a Comment
To serve static files such as images, CSS files, and JavaScript files, we can use built-in middleware function in Express. i.e. express.static
we simply need to pass the name of the folder where we have our static files, to the express.static middleware to start serving the files directly. For example, if you keep your images, CSS, and JavaScript files in a folder named public, we can use below code −
app.use(express.static('public'));
Now, you can load the files that are in the public directory:
http://localhost:3000/images/hello.jpg
http://localhost:3000/css/hello.css
http://localhost:3000/js/hello.js
http://localhost:3000/images/hello.png
http://localhost:3000/hello.html
Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL.
To use multiple static assets directories, we can call the express.static middleware function multiple times:
app.use(express.static('public'))
app.use(express.static('files'))
Express looks up the files in the order in which you set the static directories with the express.static middleware function.

To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:
app.use('/static', express.static('public'))
Now, we can load the files that are in the public directory from the /static path prefix.
http://localhost:3000/static/images/hello.jpg
http://localhost:3000/static/css/hello.css
http://localhost:3000/static/js/hello.js
http://localhost:3000/static/images/hello.png
http://localhost:3000/static/hello.html
However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve:
app.use('/static', express.static(path.join(__dirname, 'public')))


Please comment down below if you have any query and please follows us for more awesome tutorials and keep motivating us .

Node js with express framework

Leave a Comment

What is express

Express is node js web application framework which provide set of feature to develop web and mobile applications. It provides following features.

  1. Robust routing
  2. Focus on high performance
  3. Super-high test coverage
  4. HTTP helpers (redirection, caching, etc)
  5. View system supporting 14+ template engines
  6. Content negotiation
  7. Executable for generating applications quickly

How to install expressjs

$ npm install --save express
The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules.

Hello world in express js

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World From express!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})
Save above file as app.js.The app starts a server and listens on port 3000 for connections. The app responds with “Hello World From express!” for requests to the root URL (/) or route. For every other path, it will respond with a 404 Not Found.
Run the app with the following command:
$ node app.js

Then, load http://localhost:3000/ in a browser to see the output.


Hello world in express js


Please comment down below if you have any query and please follows us for more awesome tutorials and keep motivating us .

What is Node package manager

Leave a Comment
After my first tutorials which is How to create server in node js i want to explain what is node package manager. Because after this tutorials we gonna look in how's node module works. So let's start with what is node package manager.

Node package manager is online repositories for node.js packages/modules. To install node package and do version management we have to use commands.
To check current NPM version on your computer type below command
$ npm --version

If you running old version than you can update easily by running below commnand
$ sudo npm install npm -g

How to install module using npm

Using below command you can install module in node
$ npm install 
Suppose you want to install express module of node then you have to type below command.
$ npm install express

Global vs local module installation

By default npm install local module to use local module you can use require method. To check which module is install locally you use below command
$ npm ls
To install global module you can use below command.
$ npm install  -g
You can use the following command to check all the modules installed globally
$ npm ls -g

Package.Json file

by typing below command you can generate package.json file
$ npm init
If you want to save you locally module in package.json file you can type below command
$ npm install --save express
Above command will save your module name and installed version in package.json file

Uninstall module

Use the following command to uninstall a Node.js module.
$ npm uninstall express


Updating a Module

Update package.json and change the version of the dependency to be updated and run the following command.
$ npm update express


Search a Module

Search a package name using NPM.
$ npm search express


Please comment down below if you have any query and please follows us for more awesome tutorials and keep motivating us .

How to create server in node js

Leave a Comment
Hello guy's After working 3 years in node i finally decided to write tutorials on node. When i started node js i also faced so many challenge so i know how to overcome with this challenges. so let's start with first node js tutorial.

Today we will start first thing which i did in node js. Node create its own webserver so we will learn today how to create server in node js as your first application in node js.
Before we start i hope you already install node js on computer. Let's start with hello world in node js application. Node having 3 important things.


  1. Import module: In node js when we have to import any module we have to use require directive to load node module.
  2. Create server: Server which help to listen client request
  3. Read and response request: The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.

Let's start with our app.
var http = require("http");

var server = http.createServer(function(req,res){
  res.end("<h1>Hello world</h1>");
});

server.listen(3000);

console.log("Server running on port 3000");

If you copy above code in your text editor and save file as server.js and run command node server.js then you will get this output.


How to create server in node js
Now let's see what happening in our code.

var http = require("http");

we have to use require directive to use http module. http module is inbuilt module in node. so we don't need install http module we can directly use that module in our app.
var server = http.createServer(function(req,res){
  res.end("<h1>Hello world</h1>");
});
server.listen(3000);


As we talked above our second important thing is creating server. http.createServer method use for creating server and listen method is use to bind our app to server port.Above code basically create our http server which is gonna listen on 3000 port.
var server = http.createServer(function(req,res){
  res.end("<h1>Hello world</h1>");
});
server.listen(3000);


Our third step is to get request and process request to send response. In createServer function we pass parameter req,res to handle that request and process response.

when server is created createServer function call its callback function with req,res parameter. req parameter handle request and res parameter send hello world response to browser. Finally you have your first app running in your pc.

If you have any queries or you want any help or you want to request any tutorials you can comment in comment section

Please comment down below if you have any query and please follows us for more awesome tutorials and keep motivating us .
Powered by Blogger.