The improvement of the node. JS User Registration function

Source: Internet
Author: User

This article mainly implemented a crappy mail sending, log logs, and MONGODB database update operations.

Jot down the point, or leave until 51, it is not justified. Because the time is relatively tight, so many features are dealt with, left to optimize it later. e-mail this piece, the implementation of the relatively simple, the original plan is the server side for the user to generate a token, such as: HTTP://127.0.0.1:3000?KEY=W345D&USERNAME=FUHUI1, but because the server side also have to verify, fire big, Give up temporarily, use username directly.

Do not understand the idea, here is a brief introduction, the following is the registration page:


then judge the database Fuhui2 is not exist, if there is, I do not register, here is just a simple return, and did not implement the hint function. then the data sheet is inserted into the form



Then send the message to the registrant's mailbox, and update the IsValid field to 1. Because the MongoDB database does not have the concept of row and column, there may be 2 columns in a row, and another row of data has 5 fields. The form is updated to look like a isvalid in the previous image.



Finally, log the sent message:



Record the user's session, described in the previous section, and then jump to the user's home page:



The following is the main page after the jump, currently just means that there is a jump to this function, other regardless of



Don't look at this is very simple, the realization is actually not very easy, spent a full two hours, fix.


The first step, the implementation of the mail module, mainly configure the QQ mailbox, in accordance with the tutorial, Google Bar.

Exports.sendmail = function (EmailAddress, title, content) {    mail.sendmail ({        from:emailconf["user"], to        : EmailAddress,        subject:title,        html:content    }, function (Error, success) {        if (!error) {            //log record            Console.log (' message success ', success);        } else {            lib.log.write ("Maillog/sendmail.log", [err]);            Console.log (' failed ' + error);}}    );


The second step is to implement the updated function. Sometimes think that MongoDB is to JS write the same, using JS operation is particularly convenient, read PHP operation MongoDB, but the feeling is uncomfortable, JSON with an array to express, Gray often unhappy, although they are engaged in PHP.

MongoDB Gray is often magical, especially for nested arrays and documents, especially if the update operation specifies the Upsert or attributes, the lookup is not found, and the tube inserts a bar for you. What's more interesting is that the update will become a replacement, do not specify $set or $inc What, he does not update, he is the replacement operation. That's why the top one, for the first time, inserted three fields, and the second time, a IsValid field was inserted.

Paste the update for the base class:

/**     * Update user's action     * This method updated the option parameter before using the default value {safe:true}    /this.modify = function (TableName, Wherejson, Rowinfo,option, callback) {        connection (function (mdbconn) {            mdbconn.collection (tableName, function (err, Collection) {                collection.update (Wherejson, rowinfo, option, function (ERR) {                    if (err) {                        console.log (err); C9/>callback (FALSE);                    }                    else {                        callback (TRUE);}});})        });    

The third step, the log operation. Although there is log4j module understanding, but still no use, because there is no time to practice, just like PHP, write a write class to manually write, and then perfect it

Log record exports.write = function (file, infor) {    var file_path = log + file;    Lib.fs.open (File_path, "a", function (err, FD) {        if (err)            throw err;        Console.log (typeof infor);        return,//        if (typeof infor! = ' array ')//            throw new Error ("type failed");        var data = Format_time + "," + Infor.join (",") + "/n";        Lib.fs.writeSync (fd, data, function (err, writen, str) {        });        Lib.fs.closeSync (FD);    });

Fifth step, generate a token for the mailbox.   I originally planned to generate a random number, but inadvertently found that the crypto template, would have been to the module is a hash password encryption, here my encryption is also very simple easy, is the ordinary hash added a salt value. Return to the theme, found that this module can also generate a random number, so, hee,


/** * Gets a random 10-digit number */exports.validcode =function (callback) {    crypto.randombytes (10,function (ex,buf) {        if (ex) Throw ex;        Callback (BUF);    });

Carefully looked at, all is the bug, the road is still very long ah, next completes the login, then is consummates this several modules, the log, the encryption, the mail sends and so on.


After these things are put into the virtual machine, in the virtual machine installed Nginx, give up 127.0.0.1 this low name. As for the uploading of the file part, do not install VSFTPD, it is worth saying, the installation of the online tutorial is really too big fire, are saying something is not reliable, or everywhere reproduced, can not solve the problem, what Add directory permissions, add user group introduced are very uncomfortable, but also because of computer reasons, We use the RZ method directly.

Because my Redis and MongoDB are on the virtual machine, so every time you start the switch interface.



Good hard, welcome everyone to add a group of attack.



Node. JS User Registration feature complete

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.