Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

132
Views
Dynamically adding a route with Express using frontend parameter

I'm trying to build an application will allow me to take a String parameter from the frontend, and create an Express route from that. Is that possible?

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

router.post('/newAPI/:name', function(req, res, next) {
    var name = req.params.name;
    router.get('/'+name, function(req, res, next) {
        res.send({"name":""+name});
    });
});

With this, would calling localhost:3000/newApi/bob create a new route localhost:3000/bob that returns {"name":"bob"}?

8 months ago · Santiago Trujillo
1 answers
Answer question

0

It will work, unless you restart the application.

Also, just use {"name": name}.

8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs