Express.js Installation Express.js is a dependency module and should be installed in the local project node_modules folder: $ npm install express@4.8.1.. Express.js Installation Once
Trang 1Express.js
Express.js Installation
&
Express.js Generator Installation
Trang 2Express.js
Installation
Express.js is a dependency module and should be
installed in the local (project) node_modules folder: $ npm install express@4.8.1
NPM looks for either the node_modules folder or the package.json file If this is a brand new folder that doesn’t include either the folder or the file, you can create
node_modules with $ mkdir node_modules, or create package.json with $ npm init
Trang 3Express.js
Installation
Once we are inside the project folder, we can create package.json manually in a text editor or with the
$ npm init terminal command
When you use this command, you’ll
be asked to provide details such as project name, description, and so forth
Trang 4Express.js
Installation
Finally, to install the module utilizing NPM: $ npm install express The preceding command will pull the latest version of Express.js However, it’s recommended for this book that you use the version specified, so run this command instead:
$ npm install express@4.8.1 save
The save flag is optional It creates an entry for this module in package.json If you want to install Express.js for an existing project and save the dependency into the package.json file (smart thing
to do!)—which is already present in that project’s folder—run:
$ npm install express save.
Trang 5Express.js
Installation
The approach of using $ npm init and then
$ npm install express@4.8.1 save
is good if your project is blank As a result, the command will download express and its
dependencies
Trang 6Express.js
Installation
The information express@4.8.1 node_modules/express is important because it tells us where the express was placed (in node_modules) Alternatively, if the project is not blank and package.json already exists full of useful information, we can add to the package.json file our new dependency with its version number or some combination of serendipity (no recommended for production apps), e.g.,
"express": "4.8.x" or "express": "4.8.1", or "*", and run $ npm install
Trang 7Express.js
Generator
Installation
Express.js Generator (express-generator) is a separate module (since Express.js version 4.x, before which they were
bundled) It allows for rapid app creation because its scaffolding mechanism takes command-line options and generates Express.js apps based on them.
Trang 8Express.js
Generator
Installation
To install Express.js Generator, a command-line tool for scaffolding, run $ npm install -g express-generator from anywhere on your Mac/Linux machine (For Windows users, the path will be different Check the NODE_PATH environment variable and update it if you want a different global location.) The installation command will download and link the $ express terminal command to the proper path so that we can later access its command-line interface (CLI) when creating new apps Of course, we can be more specific and tell NPM to install version 4.2.0 using $ npm
install -g express@4.2.0 Versions of the express and express-generator modules don’t have to (and usually don’t) match, because they are separate modules So you need to know what’s compatible with what based
on the change log and the GitHub readme documentation.
Trang 9Express.js
Generator
Installation
Notice the path /usr/local/lib/node_modules/express-generato
r in Figure 1-4 This is drastically different from the express path of the local installation This
is one of the ways to spot an error if you’re trying to install something and it’s not available—check the location! Yes, it’s might be trivial for some readers who are well versed in Node.js, but I’ve seen too many times how some people who come from Python, Ruby and Java backgrounds try to install express (the dependency) globally so they can use it for all their projects Please don’t do it
Trang 10Express.js
Generator
Installation
Summary This chapter laid the foundation for understanding how Express.js works By
covering a typical Express.js app structure, you’ve been introduced to concepts such as configuration, middleware, and routes that we’ll cover in more depth throughout the book Most importantly, you learned how to install
Express.js locally in a blank folder and into existing projects, as well as how to install Express.js Generator In the next chapter, we’ll build our first Express.js app, the quintessential Hello World, and explore the
generator/scaffolding options