Before reading this post, please read my previous article Part 1. You can download the code here.
Assuming you are done reading the above article, we proceed with the website creation. Time to set up the express. First create a folder called, SityRoute which is our website directory, anywhere you like, open a command prompt, go to the folder.
Create a file named package.json inside the SityRoute folder, open the file, copy the below content. I am giving you my package.json file so that things will be easy for you, if you are new to
"start": "node ./bin/www"
After saving the above file under SityRoute directory, run the following command to set up the dependencies, if you are coming from the .NET site, it is a nuget package kind of stuff. If you don’t know about package.json file, then you got to do a little bit of reading node
npm packages and package.json file structure. Above dependencies json object lists out all the required dependencies in order for creation of our project.
It will give the following output at the end of the installation, shows the folder structure.
+-- firstname.lastname@example.org (email@example.com)
+-- firstname.lastname@example.org (email@example.com, firstname.lastname@example.org, email@example.com)
+-- firstname.lastname@example.org (email@example.com)
+-- firstname.lastname@example.org (email@example.com)
+-- firstname.lastname@example.org (email@example.com)
+-- firstname.lastname@example.org (email@example.com)
+-- firstname.lastname@example.org (email@example.com)
+-- firstname.lastname@example.org (email@example.com, firstname.lastname@example.org, email@example.com, firstname.lastname@example.org, email@example.com
.2, firstname.lastname@example.org, email@example.com, firstname.lastname@example.org)
Since we have mentioned express as our dependency in the above json file, it will install express version 3.4.8 along with other dependencies.
Now let us speed up the stuff. You can use express-generator to generate the scaffolding for our website, like it will create most of the ground work stuff for us like folder structures with some default hello world pages, etc. Here is how to create it.
Run the following command to first install the
$ npm install -g express-generator
It will install
express-generator globally. We are good with express generator installation. Now we need to create a sample project in which we write our code, because we are going to re-use the same folder structure.
Create a Sample Application
Once you have installed express generator, run the following command from SityRoute directory.
express -c myapp
It will ask for confirmation that destination directory is not empty, do you want to continue, say yes, the following output will be generated.
destination is not empty, continue?
destination is not empty, continue? (yes or no) y
create : .
create : ./package.json
create : ./app.js
create : ./public
create : ./public/images
create : ./public/stylesheets
create : ./public/stylesheets/style.css
create : ./routes
create : ./routes/index.js
create : ./routes/user.js
create : ./views
create : ./views/index.jade
create : ./views/layout.jade
create : ./views/error.jade
create : ./bin
create : ./bin/www
$ cd . && npm install
run the app:
$ DEBUG=my-application ./bin/www
This creates a folder SityRoute with all the necessary default code in it, by default, express generator sets the templating engine to jade engine. By now, you should have folder structure similar to the below under SiteRoute folder.
Read the next post here - Part 3 [^].