STEP 1 – INSTALLATION NODE JS
OPERATING SYSTEM | INSTRUCTIONS |
---|---|
OS X | The easiest way to install Node.js on OS X is to use the official installer from nodejs.org. You can also use Homebrew if you prefer. |
Windows | The easiest way to install Node.js on Windows is the official installer from nodejs.org. You can also use Chocolatey if you prefer. |
Linux | The exact instructions to install Node.js vary by distribution. Find instructions for yours here. |
STEP 2 – CREATE A NEW PROJECT WITH “NPM INIT”
Create a new directory in your development environment and run npm init You’ll then answer a few basic questions about your project, and npm will create a new package.json file for you when you’re done.
PS E:\Projects\demo-project> npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (demo-project) version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: license: (ISC) About to write to E:\Projects\demo-project\package.json: { "name": "demo-project", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
STEP 3 INSTALL EXPRESS
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
First let’s express install
npm install express --save
PS E:\Projects\demo-project> npm install express --save [email protected] E:\Projects\demo-project `-- [email protected] +-- [email protected] | +-- [email protected] | | `-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] | +-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] | +-- [email protected] | +-- [email protected] | | `-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] | `-- [email protected] +-- [email protected] `-- [email protected] npm WARN [email protected] No description npm WARN [email protected] No repository field.
STEP 4 – CREATE APP.JS
Create a file named app.js
and add the following code:
var express = require('express') var app = express() app.get('/', function (req, res) { res.send('Hello World!') }) app.listen(3000, function () { console.log('Example app listening on port 3000!') })
Run the app with the following command:
node app.js
Then, load http://localhost:3000/
in a browser to see the output.
Use the applIcatIon generator tool, express-generator
, QUICKLY CREATE AN APPLICATION
install express application generator.
npm install express-generator -g
PS E:\Projects> express --view=ejs myapp create : myapp create : myapp/package.json create : myapp/app.js create : myapp/routes create : myapp/routes/index.js create : myapp/routes/users.js create : myapp/public create : myapp/views create : myapp/views/index.ejs create : myapp/views/error.ejs create : myapp/bin create : myapp/bin/www create : myapp/public/stylesheets create : myapp/public/stylesheets/style.css install dependencies: > cd myapp && npm install run the app: > SET DEBUG=myapp:* & npm start create : myapp/public/images create : myapp/public/javascripts
. ├── app.js ├── bin │ └── www ├── package.json ├── public │ ├── images │ ├── javascripts │ └── stylesheets │ └── style.css ├── routes │ ├── index.js │ └── users.js └── views ├── error.ejs └── index.ejs 7 directories, 7 files