1

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}`));

6
  • 1
    do you have an existing website built in php and you want to deploy node js application on the same server? Commented Aug 21, 2020 at 12:24
  • Look at Express Routing. It shows how to setup routes to display different pages depending on the path. You can use AJAX or form submissions to send data to and process in a route before it returns the HTML/Content. expressjs.com/en/guide/routing.html Commented Aug 21, 2020 at 12:27
  • If you're following a tutorial then it most likely has all the info that you need. The HTML code you're showing would be rendered by the Express server itself using EJS. So you need to figure out how to work with EJS to accomplish what you need. Commented Aug 21, 2020 at 12:28
  • @HelloWorld I have a website, I deployed it on netlify, here is my website: physics-explained.netlify.app I dont have any php nor nodejs on the website at the moment. Commented Aug 21, 2020 at 14:25
  • @goto1 the app works perfect in localhost, I just can't seem to figure out how to get it from localhost to my website file. I would like to add the register/login on my index.html page for example. I already have a website deployed using netlify. Commented Aug 21, 2020 at 14:27

3 Answers 3

1

Node is a server-side technology so you cannot include it e.g. 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.

Sign up to request clarification or add additional context in comments.

7 Comments

I don't quite understand, I have deployed my website (using netlify), which is hosted on a server. Can't I just add the application to the website using a simple <a href> referring to the register page?
Node.js apps are not the same as static websites that are based on static files. A Node app runs as a separate application or process that serves the web pages. This cannot be done in Netlify as far as I know. My Node apps run on an Amazon virtual private server.
@goto1 I have created the app on heroku and now I have a working link where people can register and login. So do I now just link that page on my existing website? Or am I missing something obvious again?
@goto1 Or should I just host my website on heroku from now on and delete my netlify app?
You should probably implement something on the Netlify app that checks if users are logged in. Other than that, you can choose whatever you want to do.
|
0

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.

https://www.namecheap.com/support/knowledgebase/article.aspx/10202/48/how-to-install-nodejs-on-a-vps-or-a-dedicated-server

The above link has a good tutorial of how to host a nodejs server to an existing cpanel website.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.