0

I'm structuring my code to become more readable and maintainable at the same time.

here is my GET.articles and my INSERT.articles is just the same with this.

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

router.get('/sample', (req, res) => {
    res.send("Nice")
})

module.exports = router;

my index.js

const GET_articles = require('./GET.articles');
const INSERT_articles = require('./INSERT.articles');


exports { GET_articles, INSERT_articles}

and I import it like this:

app.use('/example', require('./routes/articles_controller/index'));

This is my error:

SyntaxError: Unexpected token export

2 Answers2

0

Look at your first block of code:

module.exports = router;

Do that.

Change:

exports { GET_articles, INSERT_articles}

to

module.exports = { GET_articles, INSERT_articles}

If you want to use import and export then see this question.


Then note that a plain object isn't something you can just use.

You should use the routers and not the object containing them.

const routers = require('./routes/articles_controller/index');
app.use('/example', routers.GET_articles);
app.use('/example', routers.INSERT_articles);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

app.js file

const express = require('express');
const router = express.Router();
const exampleRoute = require('./routes/articles_controller/index');
var app = express();
 app.use('/', router)

app.use('/example', router, exampleRoute);

router.get('/sample', (req, res) => {
    res.send("Nice")
})

app.listen(3000, ()=>{
    console.log("Listening on port 3000")
})

route file

const GET_articles = require('./GET.articles');
const INSERT_articles = require('./INSERT.articles');
module.exports = [GET_articles, INSERT_articles]

GET articles files

module.exports = function getArticle (req, res) {
    //Logic as required goes here
    res.send("Content to be rendered")
}
shubham
  • 414
  • 2
  • 3