Build a personal blog with Node.js+mongodb (the router.js of Hope) (four)

Source: Internet
Author: User
Tags md5 md5 encryption

The router.js that I want to write is one of my most wanted blogs now. Because he contains the essence of the entire personal blog. Here, all the business logic for the request configuration, the parameters returned, and so on is implemented in this file.

I will elaborate on the role of the code, so this blog will be a bit long, but after reading the benefit certainly also a lot!

Part I: Referencing files

Formidable is a well-known NPM package. It is a very powerful middleware in node that is processed as a form content receiver.

Db.js is a library of functions that I have written on the web for the unified operation of the database.

MD5 is a file used for data encryption (now against MD5 encryption, because it can be cracked) recommended to encrypt middleware with hashing algorithm Crypto

FS is a nodejs with a file management, all called FileSystem

Moment is a time-formatted middleware

Part II: Registering components

The app is quoted Express and then quoted in Express's Get and post methods, as the code shows: the first parameter is the routing setting the second parameter is the request handler function. The following code is a reference to the Showregister function under the router file

Exprots is a new ES6, you can expose this function outside, to refer to this function is to refer to this file

The following code refers to the

Nodejs are callbacks! Nodejs are callbacks! Nodejs are callbacks! (Important things say three times)

all request handlers will receive three parameters: request, Response (response), result (results)

The Nodejs has a built-in render function to specify the template file. The file format can only be ejs.

Doregister function

First instantiate the formidable incomingform, and instantiate the object variable named form

Reference the Parse method under the instantiated object (receives two parameters: First, req, second, function "What we have to do")

The resulting form information is in fields, the Err parameter is an error message, and files is the file information

After the value is taken, a variable store is defined. Password plus double MD5 encryption (however, it is not used, or will be cracked.) MD5 no longer safe)

Then use the DB external function Insertone to add a new data to the database, Parameters: (table name, JSON to store, callback function)

And the callback function receives two parameters (does it crash?). That's right, because I collapsed. ) Parameter: (error message, result)

If the error returns an error message, the session setting is successfully executed and the registration success information is returned

Part III: Login components

Operation and Doregister function, not the same is the query data, need to match the account and password is correct, log in successfully after the session.

Part IV: Writing article components

First visit the URL to determine if there is a session, no session is representative of not logged in, need to log in.

When the server has a session, render this template.

Write an article: Someone may wonder why adding articles to a database requires querying all the articles?

I still want to write a self-increment ID, want the article ID number. There is no self-increment ID in Nodejs. Only the random ID number.

And then the data is stored in JSON format, I have to be semantically named, you can see clearly.

Part V: Get the User location component

Here I used the query network API through the IP address to determine the visitor's current location and client information.

Because of the location of the query network is not accurate, I found a very powerful tour locator to do this function.

This is where the home page uses Ajax to send data to the Nodejs interface to store the visitor's current geographic location

Part VI: Classification components

In this classification component, the rest is the same, but when the query is more than a condition. Classify is JavaScript, it realizes the effect of article classification.

Above.

Build a personal blog with Node.js+mongodb (the router.js of Hope) (four)

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.