basicily im having problems with making the link to comments dynamic. my goal is that no matter what number i put in it the get will enter my call and give me the id i want the call looks like http://localhost:1234/comments?id=num but im not finding anything that can let my call be dynamic any help is grateful
server.js
(async () => {
const db = require("./database");
const users = await db.selectUsers();
console.log(users);
/*const assoc = await db.selectAssoc();
console.log(assoc);*/
const processRequest = require('./processRequest');
const aplicacao = processRequest(1234);
aplicacao.get('/users',function(req,res){
res.write(JSON.stringify(users));
res.end();
});
aplicacao.get('/comments?id=1',function(req,res){
console.log("entered");
res.end();
});
})();
processRequest.js
var http = require('http');
var criarProcessRequest = function(porta){
var entrada = {};// Api de entrada
var caminhos = {};// rotas para o pedido
var metodos = ['GET','POST'];// O pedido é enviado com maisculas
metodos.forEach(function(metodo){
caminhos[metodo] = {};
entrada[metodo.toLowerCase()] = function(path,fn){
caminhos[metodo][path] = fn;
};
});
http.createServer(function(req,res){
res.setHeader('Access-Control-Allow-Origin','*');
if(!caminhos[req.method][req.url]){
res.statusCode = 404;
return res.end();
}
caminhos[req.method][req.url](req,res);
}).listen(porta);
return entrada;
};
module.exports = criarProcessRequest;
also sorry if the english is bad it isnt my first languege
i have tried special characters like #,$,&,? and have tried researching
alex pestana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.