Express options


The process is extremely simple, requiring only that you invoke the generator on the command line with a new project name, optionally also specifying the site's template engine and CSS generator.

We'll also explain how the skeleton website is structured. At the end we'll show how you can run the website to verify that it works.

The Express Application Generator is not the only generator for Express applications, and the generated project is not the only viable way to structure your files and directories. The generated site does however have a modular structure that is easy to extend and understand. For information about a minimal Express application, see Hello world example Express docs.

You should already have installed the generator as part of setting up a Node development environment. As a quick reminder, you install the generator tool site-wide using the NPM package manager, as shown:. The generator has a number of options, which you can view on the command line using the --help or -h command:.

You can simply specify express to create a project inside the current directory using the Jade view engine and plain CSS if you specify a directory name then the project will be created in a sub-folder with that name. The other options for choosing template engines e. Use --view or -v! Express itself can also support a large number of other templating languages out of the box. If you want to use a template engine that isn't supported by the generator then see Using template engines with Express Express docs and the documentation for your target view engine.

Generally speaking you should select a templating engine that delivers all the functionality you need and allows you to be productive sooner — or in other words, in the same way that you choose any other component! Some of the things to consider when comparing template engines:. There are many resources on the Internet to help you compare the different options! The Express Application Generator allows you to create a project that is configured to use the most common CSS stylesheet engines: CSS has some limitations that make certain tasks difficult.

CSS stylesheet engines allow you to use more powerful syntax for defining your CSS, and then compile the definition into plain-old CSS for browsers to use.

As with templating engines, you should use the stylesheet engine that will allow your team to be most productive. For this project we'll use the ordinary CSS the default as our CSS requirements are not sufficiently complicated to justify using anything else. For the sample Local Library app we're going to build, we'll create a project named express-locallibrary-tutorial using the Pug template library and no CSS stylesheet engine.

First navigate to where you want to create the project and then run the Express Application Generator in the command prompt as shown:. At the end of the output the generator provides instructions on how you install the dependencies as listed in the package.

At this point we have a complete skeleton project. The website doesn't actually do very much yet, but it's worth running it to show how it works. You could also start the app just using the npm start command. For example, when you visit the above page you'll see debug output like this:.

Any changes you make to your Express website are currently not visible until you restart the server. It quickly becomes very irritating to have to stop and restart your server every time you make a change, so it is worth taking the time to automate restarting the server when needed. One of the easiest such tools for this purpose is nodemon. This is usually installed globally as it is a "tool" , but here we'll install and use it locally as a developer dependency , so that any developers working with the project get it automatically when they install the application.

Use the following command in the root directory for the skeleton project:. If you open your project's package. Because the tool isn't installed globally we can't launch it from the command line unless we add it to the path but we can call it from an NPM script because NPM knows all about the installed packages.

Find the the scripts section of your package. Initially it will contain one line, which begins with "start".

Update it by putting a comma at the end of that line, and adding the "devstart" line seen below:. We can now start the server in almost exactly the same way as previously, but with the devstart command specified:.

Now if you edit any file in the project the server will restart or you can restart it by typing rs on the command prompt at any time. You will still need to reload the browser to refresh the page. We could have replaced the command in the start script but we only want to use nodemon during development, so it makes sense to create a new script command. This sets up some of the application error handling and then loads app. The dependencies include the express package and the package for our selected view engine pug.

In addition, we have the following packages that are useful in many web applications:. The scripts section defines a " start " script, which is what we are invoking when we call npm start to start the server.

From the script definition you can see that this actually starts the JavaScript file. As a quick reminder, you install the generator tool site-wide using the NPM package manager, as shown:. The generator has a number of options, which you can view on the command line using the --help or -h command:. You can simply specify express to create a project inside the current directory using the Jade view engine and plain CSS if you specify a directory name then the project will be created in a sub-folder with that name.

The other options for choosing template engines e. Use --view or -v! Express itself can also support a large number of other templating languages out of the box. If you want to use a template engine that isn't supported by the generator then see Using template engines with Express Express docs and the documentation for your target view engine.

Generally speaking you should select a templating engine that delivers all the functionality you need and allows you to be productive sooner — or in other words, in the same way that you choose any other component!

Some of the things to consider when comparing template engines:. There are many resources on the Internet to help you compare the different options! The Express Application Generator allows you to create a project that is configured to use the most common CSS stylesheet engines: CSS has some limitations that make certain tasks difficult.

CSS stylesheet engines allow you to use more powerful syntax for defining your CSS, and then compile the definition into plain-old CSS for browsers to use. As with templating engines, you should use the stylesheet engine that will allow your team to be most productive.

For this project we'll use the ordinary CSS the default as our CSS requirements are not sufficiently complicated to justify using anything else. For the sample Local Library app we're going to build, we'll create a project named express-locallibrary-tutorial using the Pug template library and no CSS stylesheet engine. First navigate to where you want to create the project and then run the Express Application Generator in the command prompt as shown:.

At the end of the output the generator provides instructions on how you install the dependencies as listed in the package. At this point we have a complete skeleton project.

The website doesn't actually do very much yet, but it's worth running it to show how it works. You could also start the app just using the npm start command. For example, when you visit the above page you'll see debug output like this:. Any changes you make to your Express website are currently not visible until you restart the server.

It quickly becomes very irritating to have to stop and restart your server every time you make a change, so it is worth taking the time to automate restarting the server when needed. One of the easiest such tools for this purpose is nodemon.

This is usually installed globally as it is a "tool" , but here we'll install and use it locally as a developer dependency , so that any developers working with the project get it automatically when they install the application. Use the following command in the root directory for the skeleton project:.

If you open your project's package. Because the tool isn't installed globally we can't launch it from the command line unless we add it to the path but we can call it from an NPM script because NPM knows all about the installed packages.

Find the the scripts section of your package. Initially it will contain one line, which begins with "start". Update it by putting a comma at the end of that line, and adding the "devstart" line seen below:. We can now start the server in almost exactly the same way as previously, but with the devstart command specified:.

Now if you edit any file in the project the server will restart or you can restart it by typing rs on the command prompt at any time. You will still need to reload the browser to refresh the page. We could have replaced the command in the start script but we only want to use nodemon during development, so it makes sense to create a new script command. This sets up some of the application error handling and then loads app.

The dependencies include the express package and the package for our selected view engine pug. In addition, we have the following packages that are useful in many web applications:. The scripts section defines a " start " script, which is what we are invoking when we call npm start to start the server.

From the script definition you can see that this actually starts the JavaScript file. It also defines a " devstart " script, which we invoke when calling npm run devstart instead. This starts the same. The very first thing this does is require the "real" application entry point app. Here we specify app. The remainder of the code in this file sets up a node HTTP server with app set to a specific port defined in an environment variable or if the variable isn't defined , and starts listening and reporting server errors and connections.

For now you don't really need to know anything else about the code everything in this file is "boilerplate" , but feel free to review it if you're interested. This file creates an express application object named app , by convention , sets up the application with various settings and middleware, and then exports the app from the module.