I followed a tutorial on how to make a register and login function for a website using NodeJS, express, MongoDB as database and EJS. I included a simple HTML file where I would like to have an href that goes towards the register page. The JavaScript I included runs the server on port:5000, or on this: (process.env.PORT) variable. Basically my question is how I can include this register and login feature on my website.
btw the included HTML file is not my actual website, but is just an easy example and the JavaScript I included is not all there is to the register/login app.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
click on register below to go to the register page<br>
<a href="">register</a>
</body>
</html>
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const http = require('http');
const app = express();
// Passport config
require('./config/passport')(passport);
// DB Config
const db = require('./config/keys').MongoURI;
// Connect to Mongo
mongoose.connect(db, { useNewUrlParser: true})
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
// EJS
app.use(expressLayouts);
app.set('view engine', 'ejs');
// Bodyparser
app.use(express.urlencoded({ extended: false}));
// Express Session
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
}));
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Connect Flash
app.use(flash());
// Global variables
app.use((req, res, next) => {
res.locals.success_msg = req.flash('succes_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
});
// Routes
app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Service started on port ${PORT}`));
HTMLcode you're showing would be rendered by theExpressserver itself usingEJS. So you need to figure out how to work withEJSto accomplish what you need.