The premise is to set up a good environment, node,npm,express; (Recommended global installation)
Start creating a foundation project with Express:
Express–t Ejs Microblog
After entering the folder
Npm-install (automatic installation of dependencies to be used)
This time, with a browser access to localhost:3000, you can come out a welcome interface, here is a simple principle: when you enter this address in the browser hit the return of the moment, node set up a website daemon began to work, in app.js This file, there are App.get ('/', routes.index); the function of this sentence is to route your request '/' to the index function under the route folder when you visit '/'. We look again at the index function: Res.render (' index ', {title: ' Express '}), the function is to invoke the template engine and pass in a parameter Express. The template file is the view folder below the Index.jade, inside the sentence: h1= title P Welcome to #{title} implementation of the parameters sent over the Express package into the tittle inside, and finally achieve the final results display.
This is a complete process of request, routing, and Response.
We found in the App.js file inside and App.get ('/', route.index); similar to another routing rule: App.get ('/users ', user.list); As with the previous routing rule, we can tell that This rule means that when we visit Localhost:3000/users, the browser will return the User.list file under the Routes folder through the daemon and return it to the browser results. Sure enough, the browser does pass through this file: Res.send ("respond with a resource"); Returns respond with a resource.
This allows us to complete two complete requests and response processes.
So, we want to create a truly own process, from the response of the request to the last display on the browser page, are their own things.
Well, according to the book, we're starting to create a page that just shows the time, which is what we envision: when we enter Localhost:3000/hello in the browser and hit enter, the browser returns to us the current time.
and started.
First of all, we son ah App.js inside new
App.get ('/hello ', Routes.hello);
Then add the Hello function under the Routes folder under the Index.js file:
1 exports.hello=function(req,res) { 2 3 res.send (' The time is ' + New Date (). toString ()); 4 5 };
Finally, complete the test.
Http://localhost:3000/hello
Return:
Time is Wed Sep 14:05:34 gmt+0800 (China Standard Time)
node. JS Learning Note (6)--Create a project with Express