node. js + Express + Mongodb Development Build personal website (ii)

Source: Internet
Author: User

Second, the route

1. Open routes/index.js , which means capturing a GET request to access the home page:

and assigned to the corresponding route via app.js :

See here, open http://127.0.0.1:3000/users/to feel the meaning of a route:

routes/index.js File:

If our request is get, the path is '/' then it is rendered index.ejs this view, you can omit the suffix name. Ejs:

This means uploading the data to the Index.ejs and outputting it to the page.

2. View Views/index.ejs

You can see that the EJS fetch data is obtained by <%= data%>.

Ejs template here just remember:
  Format <% Execution Method%>
The format <%-executes the method and outputs the result to the page%>
Format <%= output data directly to the page%>

3, back to the routes/index.js We continue to talk about

Express encapsulates a number of HTTP request methods, as follows:

Router.get get requests are often used for queries and page jumps

Router.post Post requests are often used for data submission (ADD)

Router.patch is often used for data modification

Router.delete often used for data deletion

Router.use Intercept All request modes

However, in general, small projects are generally get,post,use.

If you write an API service project, you need to add and delete the request to change the way separate,

That's why you use Router.patch,router.delete.

PS: Routing is the forwarding of methods .....

4. Remove users Routes

Delete these two lines in app.js

and delete the routes/users.js file

PS: Personal Site A route is good, otherwise the project structure is too fragmented

Route first here, see tell ...

Reprint Please specify: http://www.cnblogs.com/zhangmingze/p/4995933.html

node. js + Express + Mongodb Development Build personal website (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.