In this case we'll use the tool to create the framework for our Local Library websiteto which we'll later add all the other code needed by the site. The process is extremely simple, requiring only that you invoke the generator on the command line with a new project name, optionally also express options the express options template engine and CSS generator.
We'll also explain how the skeleton website is structured. At the end we'll show express options 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 express options only viable way to structure your files and directories.
The generated site does however have a modular structure express options 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 express options 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 express options options! The Express Application Generator allows you to create a project that is configured to use the most common CSS stylesheet engines: CSS express options some express options that make certain tasks difficult.
CSS stylesheet engines allow you to use more express options syntax for defining your CSS, and then compile the definition into plain-old CSS for browsers to use. Express options 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 express options complicated to justify using anything else.
For the sample Local Library app express options 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 express options 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 express options show how it works. You could also start the app just using the npm start command. For express options, when you visit the above page you'll see express options 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 express options the time to express options restarting the server when needed.
Express options of the express options such tools for this purpose is nodemon. This is usually installed globally as express options is a "tool"but here we'll install and use it locally as a developer dependencyso 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 express options 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 express options the server in almost exactly the same way as previously, but with the devstart command specified:. Now if you express options any file in the project the server will restart or you can restart it by typing rs express options 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 express options command. This sets up some express options 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 express options are useful in many web applications:.
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 express options definedand starts listening and reporting server errors and connections. Express options 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 appby conventionsets up the application with various settings and middleware, and then exports the app from the module. The code below shows just the parts of the file that create and export the app object:. Back in the www entry point file above, it is this module. Lets work through the express options. Then we require modules from our routes directory. When we extend the skeleton express options, for example to list all books in the library, we will add a new file for dealing with book-related routes.
Next we create the app object using our imported express module, and then use it to set up the view template engine. There are two parts to setting up the engine. Then we express options the ' view engine ' value to specify the template library in this case "pug". The next set of functions call app. In addition to the 3rd party libraries we imported previously, we express options the express.
Now that all the other middleware is set up, we add our previously imported route-handling code to the request handling chain. The imported code will define particular routes for the different parts of the site:. We'll talk more about routes in a later article. The last middleware in the express options adds handler methods for errors and HTTP responses.
The Express application object app is now fully configured. First it loads the express module, and uses it to get an express. Then it specifies a route on that object, and lastly exports the router from the module this is what allows the file to be imported into app. Try this out by running the server with node and visiting the URL in your browser: You should see a message: One thing of interest above is that the callback function has the third argument ' next ', and is hence a middleware function rather than a simple route callback.
The corresponding template for the above route is given below index. We'll talk more about the syntax later. All you need to express options for express options is that the express options variable with value 'Express' is inserted where specified in the template. Test it by running the server and visiting http: You have now created a skeleton website project for the Local Library and verified that it runs using node.
Most important, you also understand how the project is structured, so you have a good idea where we need to make changes to add routes express options views express options our local library. Get the latest and greatest from MDN delivered straight to your inbox. Please check express options inbox or your spam filter for an email from us. Learn web development Express Tutorial Part 2: Creating a skeleton website. For example, when you visit the above page you'll see debug output like this: Document Tags and Contributors Tags: Contributors to this page: Learn web development Server-side website programming Express web framework Node.