NodeJs

Nodejs Express application generator

Using the express generator tool we can quickly and easy create an application skeleton.

Install express generater

1installing-express-1generator

Use express -h for help

Creating Express application with express generator

syntax

eg

2-express-generater-my-app-1

It will create the folder structure and some required files

3-express-generater-my-appfolderstruct

Go inside your app folder and  execute npm install

after-executing-npm-install

It is ready to start the server

runninf-the-server

 

 

ref : https://expressjs.com/en/starter/generator.html

 

2 Likes
By bm on October 5, 2016 | NodeJs | A comment?
Tags: , ,

Nodejs Express Debugging using Visual Studio Code

Click the debugger button located in the left side tool bar

setting debugger

Now you can see the debugger window, Click the setting icon and select Nodejs from the list

setting debugger

It will open (Create) a  launch.json, make sure that the program path locating to your app entry point file ( “program”: “${workspaceRoot}/index.js” ) ( in my case it is index.js in your case may be it is app.js or other). Save the file using Ctrl+S

setting debugger

In the bellow screen you can see the automatically created launch configuration file ‘launch.json’

Open your package.js, check the start script is there or not, if it is not there please add it.

setting debugger

Add break point in your code

setting debugger

Click the debugging button

setting debugger

Open your browser and hit the url

setting debugger

 

 

For downloading visual studio code IDE – click here

 

  Like
By bm on August 19, 2016 | Express, NodeJs | A comment?
Tags: , , ,

Nodejs Express GET POST Multipart request handling example

Nodejs Express GET POST Multipart request handling example

Create a new with nodejs and express (check this post for how to create node express project)

Pr-requirements

  1. create an empty folder in root of your application
  2. install requires node packages (I have added all the node packages in package.json so you can simply run npm install it will take care of this)

index.js this is my app entry point.

routes.js :- I have made a separate rout file that’s why the routs.js file (Check this post for how to make separate rout file) otherwise you can directly add this code into your entry point file (in my case index.js)

I am using the multer node package for handing multipart request

home.html

package.json

It is ready open your browser and test it

nodefileupload

You can see the file is uploaded, but it is using some random name for the file.

For changing file name we can make a small configuration changes in routs.js. Insted of  ‘var upload = multer({ dest: ‘./uploads/’ });’ add the new configuration settings

 

 

ref :

http://wiki.workassis.com/installing-express-with-nodejs/

http://wiki.workassis.com/nodejs-express-separate-routes/

https://www.npmjs.com/package/multer

  Like

Allow CORS in Express/Node.js?

Open index.js (in your case it may be app.js or server.js) and set the request header

 

  Like
By bm on August 17, 2016 | Express, NodeJs | A comment?
Tags: , ,

Nodejs Express separate routes

Move the rout definition from the index.js or app.js

Folder structure

node structure

index.js file

 

routes.js

you can add more routs like admin routs (*i am not added this in my project so it will not see int the above folder structure)

adminroutes.js

 

ref: https://expressjs.com/en/guide/routing.html

3 Likes
By bm on August 2, 2016 | Express, NodeJs | A comment?
Tags: , ,

Installing Express with nodejs

Installing Express with nodejs project dependency

  1. Create project folder and open the folder using  command prompt
  2. execute the following  commands
  3. Create project using  npm init command

expressjs

4. Now install Express in the app directory and save it in the dependencies list using :-  npm install express --save (To install Express temporarily and not add it to the dependencies list, omit the --save option)

expressjs

you can see the express is added to the project dependency

5. You can install the following supporting libraries also

body-parser – This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.

cookie-parser – Parse Cookie header and populate req.cookies with an object keyed by the cookie names.

multer – This is a node.js middleware for handling multipart/form-data.

6. Create a sample code

7 Run the server using node index.js open http://127.0.0.1:8000/ in your browser 

expressjs

Done

 

There a node package nodemon, “Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development”

Running our project with nodemon

nodemon

 

How to use express generator for creating project http://wiki.workassis.com/nodejs-express-application-generator/

  Like
By bm on August 1, 2016 | NodeJs | A comment?