Node js and express -- express generator

npm install -g --depth 0

npm install -g express-generator

express -h

express test project

npm install

Elevation start

 

app.js -- server/index.js

look at the routes folder

 

-------------------------

- [Instructor] In this course, we've created an express application from scratch to really understand all the bits and pieces. For real projects, you might want to check out the Express generator. It lets you create a new Express project with just one simple command. So let's try this out. For that we have to first install the Express generator so I type sudo and you only have to use sudo if you're on Linux or a Mac. NPM install dash G, because we want to install it globally, express dash generator.

Now after installing that, we have the express command available so we can type express and dash H to get the help output of it. So you see that it comes with a few options. We will now only run Express and the project name and I simply name it test project. This now created a folder, test project, for us and if I look into that, I see a package JSON that already has all the dependencies entered that we need but the node modules for this still missing, so if you want to get started with this project, you would then have to run NPM install in the project folder.

After that, we can already start it with NPM start. And let's head over to the browser and input 3000. We get a working Express application. Let's review the code real quick. So we have this app JS file. This would be like our server index chest and here you see that it brings in a few more middle vars like the cookieParser that you would use to handle cookies that come from the client.

It also installs a logger and then it requires one index route and one for users. Let's see what the user's route does. You see it does not do much right now. Noteworthy is also the WWW file in pinned Noteworthy is also the WWW file in pinned because it's the start-up file for your project, and it's slightly different from what we've done in our project. For instance, it uses debug to give you debug output and it lets you set the port as environment variable and it will also add some callback functions that handle different kinds of errors.

For the views, let's look into that. It uses park, in this case, it still calls it jade but if you look into that, you see it's regular jade syntax and it gives you also a layout file already. Going back into app JS, we see that it still uses the var statement and that the code overall does not use ES6 right now. For that I actually created a pull request for this project that should add a dash dash ES6 argument to the generator and would then give you proper ES6 compliance syntax.

Hopefully, it gets accepted soon so that you can use it for your project.

Guess you like

Origin blog.csdn.net/qq_33471057/article/details/92802692