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')))

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

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
Subscribe for more node js tutorials. I hope this will help you :D
Powered by Blogger.