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 .

How to enable mod_rewrite in apache2

Leave a Comment
In Previous tutorials we looked how to mod_rewrite in windows xammp today we are gonna learn how to enable mod_rewrite in apache2. Here is some command which we have to follow.

To check whether mod_rewrite is enabled:

Look in mods_enabled for a link to the module by running


ls /etc/apache2/mods-enabled | grep rewrite

If this outputs rewrite.load then the module is enabled. (Note: your path to apache2 may not be /etc/, though it's likely to be.)

To enable mod_rewrite if it's not already:

To Enable the module we have to execute below commands

a2enmod rewrite


Now restart apache2 server

service apache2 restart


After restart check phpinfo file in browser. you will get mod_rewrite module enable in apache2


Helpful links




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

how to enable mod_rewrite in xammp windows

Leave a Comment
When we start learning codeigniter remove index.php form url is biggest challenge for us. There is so many tutorials to remove index.php from url even i also written tutorials for remove index.php from url but there is so less tutorials which tell main steps of enable mod_rewrite in xammp. so let's start with this

There is some steps which we have to follow to check mod_rewrite is enable or not in our xammp. lets start with those steps


  1. Check mod_rewrite is enable or not: To check mod_rewrite is enable or not in xammp we have to write some php code for that purpose.
    <?php
    /**
    * to check which module is enable on server
    */
    echo phpinfo();
    ?>
    


    check in Loaded Modules or search for mod_rewrite in output. if you don't find mod_rewrite in loaded module in we have to enable that module.
  2. How to enable module: To activate the module, the following line in httpd.conf needs to be active:
    LoadModule rewrite_module modules/mod_rewrite.so
    


    enable mod_rewrite in xammp


    Remove hash form the that line and save that file after that restart your apache server and reload that page in browser. you will module is loaded in our xammp server.



You can watch video for how to enable mod_rewrite in xammp windows





Helpful links


Xammp port 443 already use issue

Leave a Comment
Hello guy's today i am going to show u how to solve already port issue in xammp. Some times when you start xammp you will get this kind of error which is shown in below image


xammp already used 443 port issue


Now how to solve this kind of issue so here is the solution follow this steps :D 


Here is the solution step-by-step: 
  1. Open up httpd-ssl.conf.  
  2. Look for the line Listen 443  
  3. Change port number to anything you want. I use 4430. ex. Listen 4430.  
  4. Replace every 443 string in that file with 4430.  
  5. Save the file.
After saving file click on start again of apache now it will be work


Xammp port 443 already use issue solve




Helpful links


Powered by Blogger.