In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces node.js how to achieve the website login registration function of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this node.js how to achieve the website login registration function article will have a harvest, let's take a look.
The effect is as follows
Project framework
Main entrance app.js
App.js is the main entry of the program, which is mainly used to write the middleware and various settings we have introduced.
Var express = require ('express'); / / Path object in NodeJS, used to process directory objects to improve development efficiency var path = require (' path'); / / middleware var favicon = require ('serve-favicon') used to define web page logo; / / Express framework in NodeJs uses morgan middleware to log / / the app.js file in / / Express has introduced the middleware var logger = require (' morgan') by default / / use app.use (logger ('dev')) to print the request information on the console for development and debugging, / / but in the actual production environment, you need to record the log in the log file var logger = require (' morgan'); / / store login information middleware var cookieParser = require ('cookie-parser'); / / parse request middleware var bodyParser = require (' body-parser') / / js file var routes = require ('. / routes/index'); / / var users = require ('. / routes/user'); / / introduce session middleware var session=require ('express-session'); / / create project example var app = express (); / / introduce the template we need app.set (' views', path.join (_ _ dirname, 'views'); app.set (' view engine', 'ejs')) / / record request app.use (logger ('dev')); app.use (bodyParser.json ()); app.use (bodyParser.urlencoded ({extended: false})); / / access information app.use (cookieParser ("Luck")) using cookieParser middleware; / / access information app.use (session ({secret:'luck', resave:false, saveUninitialized:true}) using session middleware) / / static the files under our public file so that they can directly reference app.use (express.static (path.join (_ dirname, 'public')); app.use (' /', routes); / / app.use ('/ users', users); / / capture 404 state app.use (function (req, res, next) {var err = new Error ('Not Found'); err.status = 404; next (err);}); module.exports = app The index.js file index.js under app.listen (3000) routes. Here I use to handle the route jump of the page var express = require ('express'); var router = express.Router (); / / the js file linked to the database, which can query the information such as user name and password in the database var usr=require (' netRequest/dbConnect'). / / get the home login information router.get ('/', function (req, res) {if (req.cookies.islogin) {req.session.islogin=req.cookies.islogin;} if (req.session.islogin) {res.locals.islogin=req.session.islogin;} res.render ('index', {title:' HOME',test:res.locals.islogin});}) / / the login page processes router.route ('/ login') / / get request to render the page. Get (function (req, res) {if (req.session.islogin) {res.locals.islogin=req.session.islogin;} if (req.cookies.islogin) {req.session.islogin=req.cookies.islogin;} res.render ('login', {title:' user login', test:res.locals.islogin}) }) / / post request to query user information. Post (function (req, res) {client=usr.connect (); result=null; / / call database method usr.selectFun (client, req.body.username, function (result) {if (result [0] = undefined) {res.send ('no user')) } else {if (result [0] .password = = req.body.password) {req.session.islogin=req.body.username; res.locals.islogin=req.session.islogin; res.cookie ('islogin',res.locals.islogin, {maxAge:60000}); res.redirect (' / home');} else {res.redirect ('/ login');}) }); / / exit login page processing router.get ('/ logout', function (req, res) {res.clearCookie ('islogin'); req.session.destroy (); res.redirect (' /');}); / / home page processing router.get ('/ home', function (req, res) {if (req.session.islogin) {res.locals.islogin=req.session.islogin;} if (req.cookies.islogin) {req.session.islogin=req.cookies.islogin) } res.render ('home', {title:' Home', user: res.locals.islogin});}); / / Registration page processes router.route (' / reg') / / get request rendering page. Get (function (req,res) {res.render ('reg', {title:' Registration'});}) / / post request registered user .post (function (req,res) {client = usr.connect () / / call the database method usr.insertFun (client,req.body.username, req.body.password2, function (err) {if (err) throw err; res.send ('registered successfully');}); module.exports = netRequest/dbConnect.js in router;node_modules
DbConnect.js
Var mysql=require ('mysql'); / / now it's just an exercise to create links directly for the database. / / users often need to create a connection pool function connectServer () {var client=mysql.createConnection ({host:'172.16.20.103', port:3308, database:'test', user:'JRJ_Win', password:' FT% ^ $fjYR56'}) return client } function selectFun (client,username,callback) {client.query ('select password from win.luck_user where username= "' + username+'', function (err,results,fields) {if (err) throw err; callback (results);}) } function insertFun (client, username, password,callback) {client.query ('insert into win.luck_user value (,)', [username, password], function (err,result) {if (err) {console.log ("error:" + err.message); return err;} callback (err);} exports.connect = connectServer;exports.selectFun = selectFun;exports.insertFun = insertFun
The rest is the page template.
Login.ejs
User: logged in.
Log out login user name password login
Index.ejs
User: you have logged in, please log in and check
Reg.ejs
User name and password registration
Header.ejs
Test Toggle navigation Project name pending Action Another action Something else here Nav header Separated link One more separated link Home Page (current) login registration
Footer.ejs
This is the end of the article on "how to achieve website login and registration in node.js". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve website login and registration function in node.js". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.