0

My goal is to create an API that handles multiple requests. By doing this, I need to pass a string as an argument to the url of API like this:

// index.js in client
fetch(`http://localhost:4000/routerName/${tableName}`).then()

// router.js
router.get(`/${tableName_from_client_page}`, (req, res) => { // Do Something })

A problem is, the browser can't connect to the targeted pages unless I create a whole new APIs for every matching tableNames.

I want my API handles multiple requests by receiving the tableName as its /url.

Are there some tricks to solve this problem?

This is how my whole router looks like:

// Router
const express = require('express'),
      db = require('./db.js'),
      router = express.Router();

router.get('/table', (req, res) => {
  db.loadTable('SELECT * FROM someTable', res);
}) // Handles only one request on the name of url; /table

router.get(`/${tableName_from_client_page}`, (req, res) => {
  db.loadTable(`SELECT * FROM ${tableName_from_client_page}`, res)
}) // Handles multiple requests, depending on its argument.

module.exports = router;
sniffingdoggo
  • 398
  • 4
  • 16
  • 2
    you need [path parameters](https://expressjs.com/en/guide/routing.html): `Route path: /users/:userId/books/:bookId` – Manuel Spigolon Jul 14 '20 at 08:24
  • Just a friendly reminder: Be aware of SQL Injections if this api will become public. https://stackoverflow.com/questions/332365/ – kai Jul 14 '20 at 08:29

2 Answers2

1
// Router
const express = require('express'),
      db = require('./db.js'),
      router = express.Router();

router.get('/table', (req, res) => {
  db.loadTable('SELECT * FROM someTable', res);
}) // Handles only one request on the name of url; /table

router.get('/tables/:tableName', (req, res) => {
  db.loadTable(`SELECT * FROM ${req.params.tableName}`, res)
}) // Handles multiple requests, depending on its argument.

module.exports = router;
slawciu
  • 775
  • 4
  • 15
1
    // Router
    const express = require('express'),
          db = require('./db.js'),
          router = express.Router();

This API will only handle one request "/table".

    router.get('/table', (req, res) => {
      db.loadTable('SELECT * FROM someTable', res);
    })

To handle multiple requests checkout below code but make sure to write this API last in the route file, If you write this API before the "/table" API then your "/table" request will also be handled by this API.

    router.get('/:table_name', (req, res) => {
      db.loadTable(`SELECT * FROM ${req.params.table_name}`, res)
    }) 
        
    module.exports = router;
Dhairya
  • 33
  • 5
  • Thanks, but is this normal that the colon follows before the table name? When I log the `req.params.table_name` the server throws like this; `{ table_name: ':Customer' }`. I could just remove this by using `split()` but still it's a bit tedious to me. – sniffingdoggo Jul 14 '20 at 09:57
  • Yes, If you write colon in the path, express will recognize it as a parameter. You can use the passed value by `req.params.table_name`. Please check out this link and find `Route parameters` https://expressjs.com/en/guide/routing.html – Dhairya Jul 14 '20 at 10:05
  • Maybe you've logged only `req.params`. It will give the object of all req.params. `req.params.table_name` will only give the table name you passed in the request. – Dhairya Jul 14 '20 at 10:12