Multiple routes matched at the same time - Strange Express behavior - node.js

I am wondering if the following express behavior is valid :
In this example, making a GET request "GET /count" will trigger/execute both routes /count and /:mail.
var express = require('express')
var app = express()
app.get('/count', function (req, res) {
res.send('Hello World!1')
})
app.get('/:mail', function (req, res) {
res.send('Hello World!2')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
When the /:mail is triggered, the output will be : "Error: Can't set headers after they are sent."
My question is : Isn't it obvious for express to match only one single route at once ?

Related

Telegram webhook integration to express app

So on the Telegraf api they have this example.
const Telegraf = require('telegraf')
const express = require('express')
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.on('text', ({ replyWithHTML }) => replyWithHTML('<b>Hello</b>'))
bot.telegram.setWebhook('https://----.localtunnel.me/secret-path')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.use(bot.webhookCallback('/secret-path'))
app.listen(3000, () => {
console.log('Example app listening on port 3000!')
})
And it clearly work fine because i can see that i get post requests on /secret-path route.
But what i don't understand is how do i work with those requests.
As far as i understand how this is working is that app.use(bot.webhookCallback('/secret-path')) creates this route in my express app.
Anyway, the question is, how do i work with requests that come to this route e.g. if i want to console.log request.body and so on.
The solution seems to be in this function. If body-parser.json() is used.
app.post(`/secret-path`, (req, res) => {
console.log(req.body)
return bot.handleUpdate(req.body, res)
})
Correct me if i'm wrong.

How to get full dynamic URL with expressJS

I'm building a middleware to check acl, but I cannot find a way to get the URL of dynamic routes. Example : '/user/:id/movie/:name'. I'm able to get the url but it's already filled with the id and name, how can I get the raw URL for this dynamic routes ? (I already checked baseUrl, originalUrl, url)
So express doesn't know what is the end dynamic URL due to router. Each route can be appended to any URL prefix.
in your case:
user/:id - is the prefix
movie/:name - is the route
const express = require('express')
const app = express()
const port = 3000
// add empty urlTemplate on each incoming request
app.use((req, res, next) => {
req.urlTemplate = "";
next();
});
// create route
var router = express.Router()
const urlTemplateMove = '/movie/:name';
const moveRoute = router.get(urlTemplateMove, function (req, res) {
req.urlTemplate += urlTemplateMove; // <- append current route urlTemplate
console.log(req.urlTemplate);
res.json(req.urlTemplate);
});
// append urlTemplate on each route
const urlTemplateUser = '/user/:id';
app.use(urlTemplateUser, (req, res, next) => {
req.urlTemplate += urlTemplateUser;
moveRoute(req, res, next);
});
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
Note if you're not interested in using router you just create the URL as in a higher scope and then you will have access to it in the callback.
const express = require('express')
const app = express()
const port = 3000
const urlTemplate = '/user/:id/movie/:name';
app.get(urlTemplate, function (req, res) {
console.log(urlTemplate);
res.json(urlTemplate);
});
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

I'm new with API's. I'm working with rest API's in node js. I'm not getting how to go from one route to another?

In the example below, if I'm in localhost:5000/A and i want to go to localhost:5000/B, but from within localhost:5000/A, how can i do that via a button ?
var express = require('express');
var app = express();
// Our first route
app.get('/', function (req, res) {
res.send('Hello Index!');
});
// Our A route
app.get('/A', function (req, res) {
res.send('Hello A!');
});
// Our B route
app.get('/B', function (req, res) {
res.send('Hello B!');
});
// Listen to port 5000
app.listen(5000, function () {
console.log('Dev app listening on port 5000!');
});
Just try this :
app.get('/A', function (req, res) {
res.send('Hello A! <button>Click me</button>');
});
Or you could serve an HTML page containing this code.
Hope this answers your question. If not please reach back to me.

Can get('/', function (req, res){} can't be called server

I was working on fetching data from mysql and using it to create d3js graph on node.js. I learnt that server can only be made from modules 'http' and 'request'. If I am not using them and using
app.get('/', function (req, res) {
res.render('index', {
//
});
});
app.listen(port, function () {
console.log('running server on port ' + port)
});
They are solving the same purpose.
What concept of server am I missing? What are the limitations of fetching data this way?
use express web framework :
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
let port=3000;
app.listen(port, function () {
console.log('running server on port ' + port)
});

App.js to redirect to a module

I am debugging into a NODE JS application and I am very new to node js. I have a REST module file
students.js
module.exports = function (service) {
/**
* Retrives data from DB
*/
service.get('/mobile/students', function (req, res) {
res.set('Content-Type', 'application/json')
.status(200)
.json(DBHelper.getAllStudents());
});
service.post('/mobile/students', function (req, res) {
res.status(200).json(data);
});
});
To run it locally I am using the following app.js
const express = require('express');
const app = express();
var routes = require('./students');
app.get('/', function (req, res) {
res.send('Hello World!')
});
app.listen(3010, function () {
console.log('Example app listening on port 3010!')
});
When I hit
http://localhost:3010/students, I am hitting a 404.
How do I explicit route the path to the student modules?
you need to add routes(app); line after var routes = require('./students'); then Your routes will be mounted..
http://localhost:3010/students if use this it will prompt you again with 404 but if you use http://localhost:3010/mobile/students it will produce desire output..

Resources