Full series: Building website with Nodejs - A post from my experience
Install Nodejs, npm using nvm
There are several ways of installing Nodejs. You can install it using your OS packages manager or compiling it from source. However, the recommended way is to install it using nvm. nvm can help you install, maintain and run multiple versions of Nodejs (without sudo need).
To install nvm, simply do
Add this to your shell’s rc file
Next, install one version of nodejs that you want using nvm and set it as the default
Installing nodejs with nvm will automatically install npm (packages manager for Node). For more information about nvm, visit it’s homepage at https://github.com/creationix/nvm.
To test whether nodejs works correctly, you can use the example from nodejs page. Create a file named example.js
and then run it using nodejs
Auto reload app with nodemon
Usually, every time you change your code, you have to manually restart node
server in order for the changes to take effect. Nodemon
is a utility that will
monitor for any changes in your source and automatically restart your server.
Just use nodemon
instead of node
to run your code. To install it
Install Express and generate the website structure
Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications. You can install Express with npm
Next, use the newly installed express to generate basic site structure. Replace
myapp
in the command below with your desired app name
The first command will generate a skeleton for your web app with session,
stylus css and
ejs render engine support. The second one will install
all the dependencies that you app needs. To run the website, execute app.js
using nodejs or nodemon
Open your web browser and go to http://localhost:3000. You should see something like this