A
A
Artem Zenevich2018-12-16 21:58:20
Node.js
Artem Zenevich, 2018-12-16 21:58:20

Express js what's wrong with routing?

Good day.
Guys, tell me what's wrong with my code, I wrote so to speak on the off-doc.
At first I sinned on the EJS template engine, but after disabling it, nothing has changed.
Just in case, the logic of actions was the following:
1 There is an index.ejs page ( if it is a view ), it has a link to the auth.ejs
registration page 2 There, respectively, a form with the POST
method 3 Submit and it's done ...
But !!! When you click on the link, everything is sad...
Cannot GET /auth
THANK YOU IN ADVANCE!!!

const express = require('express');
const bodyParser = require('body-parser');

const routes = require('./routes/index');

const app = express();

// use EJS in views
app.set('view engine', 'ejs');

// configure app
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// routes
app.get('/', (req, res) => res.render('index'));
app.use('/auth', routes.auth);

module.exports = app;

They are imported accordingly from here /routes/index.js .
const auth = require('./auth');

module.exports = {
  auth
};

Actually the auth route itself .
const express = require('express');
const router = express.Router();

router.get('/auth', (req, res) => res.render('/auth'));
router.post('/auth', (res, req) => {
  console.log(req.body);
  res.json({
    ok: true
  });
});

module.exports = router;

Z.Y. Sorry for asking this question, this is my first time here.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-12-16
@artixen

make router auth like this:

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => res.render('/auth'));
router.post('/', (res, req) => {
  console.log(req.body);
  res.json({
    ok: true
  });
});

module.exports = router;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question