expressjsHow do I create a login form using Express.js?
Creating a login form using Express.js is easy. Below is an example code block that will generate a basic login form.
// Require the Express Module
var express = require('express');
// Create an Express App
var app = express();
// Require body-parser (to receive post data from clients)
var bodyParser = require('body-parser');
// Integrate body-parser with our App
app.use(bodyParser.urlencoded({ extended: true }));
// Require path
var path = require('path');
// Setting our Static Folder Directory
app.use(express.static(path.join(__dirname, './static')));
// Setting our Views Folder Directory
app.set('views', path.join(__dirname, './views'));
// Setting our View Engine set to EJS
app.set('view engine', 'ejs');
// Routes
// Root Request
app.get('/', function(req, res) {
// This is where we will retrieve the users from the database and include them in the view page we will be rendering.
res.render('index');
})
// Add User Request
app.post('/users', function(req, res) {
console.log("POST DATA", req.body);
// This is where we would add the user from req.body to the database.
res.redirect('/');
})
// Setting our Server to Listen on Port: 8000
app.listen(8000, function() {
console.log("listening on port 8000");
})
This code will generate a basic login form with a route for posting the login data. The code consists of the following parts:
- Require the Express Module: This line imports the Express module into the program.
- Create an Express App: This line creates an instance of an Express App.
- Require body-parser: This line imports the body-parser module, which is used to receive post data from clients.
- Integrate body-parser with the App: This line integrates the body-parser module with the Express App.
- Require path: This line imports the path module, which is used to access the directory of the program.
- Setting our Static Folder Directory: This line sets the directory where the static files are stored.
- Setting our Views Folder Directory: This line sets the directory where the view files are stored.
- Setting our View Engine set to EJS: This line sets the view engine to EJS.
- Routes: This section contains the routes for the program, including the root request and the add user request.
- Setting our Server to Listen on Port: This line sets the port for the server to listen on.
Helpful links
More of Expressjs
- How do I find Express.js tutorials on YouTube?
- How do I set the time zone in Express.js?
- How can I use the x-forwarded-for header in Express.js?
- How do I set up a YAML configuration file for a Node.js Express application?
- How can I disable the X-Powered-By header in Express.js?
- How can I use express-zip js to zip and download files?
- How do I use Express.js to parse YAML files?
- How can I render HTML pages using Express.js?
- How do I implement CSRF protection in an Express.js application?
- How can I set up X-Frame-Options in ExpressJS?
See more codes...