Installing node.js is very simple and it happens in just some clicks.
Click on the Macintosh installer on the download screen.
Follow the instructions to install Node in your mac, Finally, it prompts to make sure the $path is same as node.js is referring to.
To verify, open terminal and type echo “$PATH”.
Usually, it will be set to the same path, if not or if you changed the folder of installation then add the path to the $PATH variable.
To clarify that node is installed open terminal and type node and press enter.
You will see ‘>’ mark waiting for the commands as in the above image. Try logging the usual “Hello World!” in the terminal.
If you see the result as shown then we have reached half way.
Pro tip:- to stop the running node server do Ctrl+c twice.
Express.js
Now let’s install express.js
When we install node it installs NPM by default, which is used for downloading and maintaining the dependencies and other node modules.
NPM stands for Node Package Manager.
Installing and running express in your application can be done in many ways but I prefer doing it with express-generator since it is very useful in creating application boilerplate or folder structure which makes the app development ease.
1 | Run $ npm install express-generator -g |
Do not worry if it shows some errors or debugs.
Verify the installation by executing
1 | $ express -h |
if its installed properly then you will see this,
Unfortunately, I faced a problem while installing Express,
When I executed the $ express -h command, i got
1 | express :command not found. |
If you’re also facing the same problem, do not panic I have found the solution. Run the generator command with sudo
1 | $ sudo npm install express-generator -g |
the terminal will expect the password to proceed, once the installation is a success you will see
1 2 3 4 5 6 7 8 | /usr/local/bin/express -> /usr/local/lib/node_modules/express-generator/bin/express /usr/local/lib └─┬ express-generator@4.13.4 ├─┬ commander@2.7.1 │ └── graceful-readlink@1.0.1 ├─┬ mkdirp@0.5.1 │ └── minimist@0.0.8 └── sorted-object@2.0.0 |
Now you can again verify by executing express -h.
Navigate to the folder where you want your app files to be stored.
Create an App
Create your app structure with express by running
1 | express myapp |
terminal will clearly display the app structure created by the express generator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | create : myapp create : myapp/package.json create : myapp/app.js create : myapp/public create : myapp/public/javascripts create : myapp/public/images create : myapp/routes create : myapp/routes/index.js create : myapp/routes/users.js create : myapp/public/stylesheets create : myapp/public/stylesheets/style.css create : myapp/views create : myapp/views/index.jade create : myapp/views/layout.jade create : myapp/views/error.jade create : myapp/bin create : myapp/bin/www install dependencies: $ cd myapp && npm install run the app: $ DEBUG=myapp:* npm start |
It also tells us what needs to be done next, Install the dependencies by traversing to the app folder and executing the npm install command as shown at the end. It will install all the required modules for express.
Run your first express app with the following command
1 | $ DEBUG=myapp:* npm start |
If the app is running successfully you will see the below message in the terminal
1 2 3 4 5 6 7 | > myapp@0.0.0 start /Users/user/WebDevelopment/Node/myapp > node ./bin/www myapp:server Listening on port 3000 +0ms GET / 200 400.862 ms - 170 GET /stylesheets/style.css 200 5.275 ms - 111 GET /favicon.ico 404 34.712 ms - 1295 |
To verify,
Open any browser and traverse to
http://localhost:3000/
If you see the below image then you have completed the installation and your app is running successfully. Look forward to start coding for your application.
Output
You will need:-To stop the application, just do Ctrl+c on terminal.
Happy Coding……:)