This is a step-by-step review of the process of creating a new website that uses the Express framework that runs on Node.js This Codelab assumes that you have access to a Linux machine that has Node.js installed. |
|
The default application will have no connection to a database or an authentication scheme, although you can add that later.
This tutorial uses Handlebars as the templating engine for the application, although you can choose any other engine instead.
Proceed to the directory where you want to store your website's working files.
Install the Express Generator with the following command:
npm install -g express-generator
Create the scaffold for your new site with the following command. This will start a site named website_name that uses the Handlebars view engine.
express --view=hbs website_name
There are a variety of files that you will want to add to the scaffold that was created:
touch .gitignore
touch .env
touch start.js
touch README.md
Add the following lines to your .gitignore file.
node_modules
.env
Your README file uses the Markdown syntax to add context and notes about the application you are developing. You should add a brief overview of the project here.
The start.js file is a script that allows you to configure your application's launch configuration. It's a good practice to keep this configuration separate from the actual app commands located in app.js.
The following is a very common start.js file:
const app = require('./app');
const server = app.listen(8081, () => {
console.log(`Express is running on port ${server.address().port}`)
});
Express and Node have a number of packages that must be included in order to serve your website.
Install those now with this command:
npm install
To ensure that you have all of the most updated security patches for all of your packages, also do the following:
npm audit fix
nodemon is a package that continuously monitors the source files within your application and automatically restarts the Node server when changes are detected.
nodemon is intended to be used only during development, so you will install it for development environments only:
npm install -D nodemon
Update the scripts section of your package.json file to allow the use of nodemon
"scripts": {
"start": "node start",
"dev": "nodemon --watch"
}
Before proceeding with development of your site, you should ensure that you are tracking your development locally with git and are archiving your work at GitHub or GitHub.IU.
Use this tutorial to help you start and maintain your local and remote repositories.
You now have a basic Node / Express / Handlebars template site set up. Go do something cool with it.