QuantumRich
QuantumRich

Reputation: 886

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"}?

Upvotes: 0

Views: 139

Answers (1)

skiilaa
skiilaa

Reputation: 1282

It will work, unless you restart the application.

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

Upvotes: 1

Related Questions