Home >Web Front-end >Front-end Q&A >How to register, log in and jump to the page in nodejs
Node.js implements registration, login and page jump
Node.js is a JavaScript running environment based on the Chrome V8 engine. It can run on the server side, and can be used to easily implement some common server-side functions, such as creating HTTP servers, implementing Socket.IO real-time communication, etc. In this article, we will use Node.js as the basis, use the Express framework and MongoDB database to implement a simple registration, login and page jump function.
First, we need to install Node.js locally. You can download the Node.js file corresponding to the current operating system through the official website (https://nodejs.org) and then install it.
Next, we need to create a project locally. You can enter the following instructions on the command line:
mkdir node-login
cd node-login
Run the following instructions to Initialize the project:
npm init
Enter the project name, version number, description and other information according to the prompts, and then create a package.json file.
Next, we need to install dependencies such as Express, Mongoose and Body-parser. You can enter the following instructions on the command line:
npm install express mongoose body-parser --save
--The save parameter means to save these dependencies to the package.json file.
In this example, we use MongoDB as the database. You can download MongoDB from the MongoDB official website (https://www.mongodb.com/) and install it. Then create a database and user to connect to.
Next, we need to create a server file. You can create a file named server.js in the project root directory. In this file we need to load dependencies, connect to the database and create an HTTP server to listen for requests.
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
mongoose .connect('mongodb://your-mongodb-url', { useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
if (err) {
console.log(err);
} else {
console.log('Connected to the database');
}
});
app.get('/', (req, res) => {
res.send('Hello World!') ;
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log( Server running on port ${port}
);
});
We use mongoose to connect to the MongoDB database and output logs in case of errors. Next, we create a simple route to test whether the server is functioning properly.
Next, we need to create a user data model. Create a file named user.js in the project root directory. In this file, we define a data model called User, which includes fields such as username, email, and password.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {
type: String, required: true, unique: true
},
email: {
type: String, required: true, unique: true
},
password: {
type: String, required: true
}
});
const User = mongoose. model('User', UserSchema);
module.exports = User;
Next, we need to create Registration and login routing. Add the following route in server.js:
const User = require('./user');
// Register
app.post('/register', (req , res) => {
const user = new User({
username: req.body.username, email: req.body.email, password: req.body.password
});
user.save((err) => {
if (err) { console.log(err); res.status(500).send('Error registering new user please try again.'); } else { res.redirect('/login'); }
}) ;
});
// Login
app.post('/login', (req, res) => {
const email = req.body.email;
const password = req.body.password;
User.findOne({ email: email }, (err, user) => {
if (err) { console.log(err); res.status(500).send('Error on the server.'); } else { if (!user) { res.status(404).send('User not found.'); } else { user.comparePassword(password, (err, isMatch) => { if (isMatch && !err) { res.redirect('/dashboard'); } else { res.status(401).send('Password is incorrect.'); } }); } }
});
});
These routes handle requests and create and authenticate users when they register and log in, then jump to the appropriate page.
Finally, we need to create views and templates. You can create a folder named views in the project root directory and create the following files under the folder:
In these templates, we use HTML, CSS and JavaScript to create beautiful and easy-to-use pages.
Now, we can start the project using the following command:
node server.js
In the browser Visit http://localhost:3000 to view the web page. Enter the registration information and log in to jump to the dashboard page.
Summarize
In this article, we use Node.js, Express framework and MongoDB database to create a simple application that registers, logs in and jumps to the page. Using Node.js and related technologies, you can easily and quickly create applications that implement certain server-side functions, greatly improving the efficiency of development and deployment.
The above is the detailed content of How to register, log in and jump to the page in nodejs. For more information, please follow other related articles on the PHP Chinese website!