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}`));
Node is a server-side technology so you cannot include it eg as a script file. An option for hosting server-side apps is Heroku .
Also, usage depends a bit on whether it's a headless API or a traditional web app.
In order to deploy a nodejs application to an existing web server running PHP, a good way is you can create a subdomain and host the nodejs app on the server and then redirect the login and register feature to that subdomain.
The above link has a good tutorial of how to host a nodejs server to an existing cpanel website.
I hosted the app (and now my entire website) on heroku, because netlify (the host for my website I used before) doesn't support nodeJS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.