Tiffany
Tiffany

Reputation: 31

How to add multiple pages in node js

I want to create a login and registration page for a web application. I have been using this tutorial:

https://thinkster.io/mean-stack-tutorial

However, I want to modify it so everything is not in the same index.ejs file (I do not just want to use "include") but rather on a separate page entirely. In order to do this, I created a register-login.ejs file and a register-login.js file in addition to the index.js file. In my "register-login.js" file I added:

`router.get('/register-login', function(req, res, next) {
        res.render('register-login');
});

` Then in my app.js file I added:

var routes = require('./routes/register-login');app.use('/register-login', routes);

Unfortunately, this attempt results in a 404 Error. How do I properly separate the provided code in the above tutorial into two separate pages: index.js and register-login.js, index.ejs and register-login.ejs and connect them properly?

Upvotes: 2

Views: 4165

Answers (1)

user2950720
user2950720

Reputation: 941

register extra routes on the router.

var app = express();
var router = express.Router();

router.get('/register', function(req, res, next) {
        res.render('./views/register');
});

router.get('/login', function(req, res, next) {
        res.render('./views/login');
});

then use them all with app.use('/', router);or app.use(require('./routes/user')); if you export the router and require it

Upvotes: 1

Related Questions