Getting whole query param in node.js [duplicate] - node.js

This question already has answers here:
get url after "#" in express.js middleware request
(3 answers)
Closed 3 years ago.
I have an endpoint like this:
app.post('/sendCode', async (req, res) => {
const { code } = req.body;
});
And url like this:
http://localhost:3000/sendCode?code=ABCDEFG%##HIJKLMNOPRS
So when I enter this link in the browser I will trigger the endpoint. This works. However my code taken from req.body cuts out the second part of the code. So in my endpoint I see only ABCDEFG% part, the second is cut off. How can I get whole ABCDEFG%##HIJKLMNOPRS?

The browser won't send the part of the URL that starts with hash # symbol. The information that follows the hash (URL fragment) is intended for Javascript code executed by the client.
More on this topic here: Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

Related

Does Express access an app.get(/:params) if the user didn't type anything in the URL [duplicate]

This question already has an answer here:
How does Express know which Router path to use when multiple paths match?
(1 answer)
Closed 1 year ago.
My code goes like this:
app.get("/:pageName",function(req,res){
const listName=req.params.pageName;
console.log(listName)
const list = new List({name:listName,items:defaultItems});
list.save();
});
If I only typed in the URL bar in the browser "http://localhost:3000/" does the app execute the previous code or not?
If it does, how can I tell it not to?
The short answer is no - express will only resolve a non-empty value as a path parameter, unless you use some wildcard (e.g, ?) to make it optional.

Storing the URL directory in a variable in Node/Express [duplicate]

This question already has an answer here:
Express.js route parameter with slashes [duplicate]
(1 answer)
Closed 2 years ago.
The following gives the output of the url/id.
app.get('/:id', function(req, res) {
res.send('with slash' + req.params.id);
});
However, when I'm dealing with multiple slashes in the URL, I'm unable to get the id. For example, I want url/id1/id2/etc.../id. What should I put in place of the /:id for this?
You can use multiple parameters in your URL.
/:id1/:id2

Testing multiple URLs using Nightwatchjs & Request

I'm using the Request package with my Nightwatchjs setup to test the status codes of a number of URLs (about 50 in total).
My issue is twofold.
Firstly, my code is currently as such (for a single URL);
var request = require('request');
module.exports = {
'Status Code testing': function (statusCode, browser) {
request(browser.launch_url + browser.globals.reviews + 'news/', function (error, response, body) {
browser.assert.equal(response.statusCode, 200);
});
},
};
but it's failing with a
✖ TypeError: Cannot read property 'launch_url' of undefined
So my first question is, how can I incorporate browser.launch_url + browser.globals.reviews + 'news/' into the script for a request?
Secondly, I have a list of about 50 URLs that I need to test the status code of.
Rather than repeat the code below 50 times, is there a more succinct, readable way of testing these URLs?
Any help would be greatly appreciated.
Many thanks.
The correct call you should be making is browser.launchUrl to access the API then you can concatenate the appending string paths.
Also, I believe you may be mistaking the purpose of Nightwatch. Nightwatch isn't an API testing tool, it's used to test UI as it relates to end-to-end testing. While you can incorporate some data validation to supplement your UI testing with Nightwatch, there are better options out there. But your question was how to do so with Nightwatch, so you don't have to repeat it over and over again. I created a function for my GET requests and a separate function for my POST requests. My get functions would pass in a token for authentication, and my POST requests would pass in a token as well as the payload (in my case JSON). Hope this helps you out

how to fetch req.params whatever it was

I'm working on a url shortener api. The problem i'm facing is that if I pass a parameter like https://www.youtube.com/watch?v=8aGhZQkoFbQ then req.params.url will only be equal to https://www.youtube.com/watch. I've looked a lot on stackoverflow and all the answers are similar but not what I'm looking for.
I want to parse the url parameter and get the characters it contains.
This is the URI i'm using right now
router.route('/add/:url(*)')
You can try something like this:
app.get(/[/]add[/].*/, function (req, res) {
var uri = req.originalUrl.replace(/^[/][^/]*[/]*/, '');
console.log(uri);
res.end();
});
Maybe you're wanting the query part of the URL? Take a look at req.query docs (and other parts of the request) and read about the parts of the URI or more formal definitions on wikipedia. Having the correct names will help you understand the Express.js docs.
From express.js docs:
// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret"
From wikipedia:
scheme:[//[user:password#]host[:port]][/]path[?query][#fragment]

Timer based on Express JS GET/POST events

I am completely lost on this one. I have a simple app that asks 6 questions to a user. Each question and the answers available are shown on a dynamically generated view for a particular questions route. I want to set up a time that works as follows: When question 1 shows, the user has 5 minutes to answer all 6 questions. The timer would clear once the POST for question 6 occurs.
The basis for the GET/POST code is as follows (using the ejs model):
app.get('/survey/:question_number?', restrict, routes.survey);
app.post('/survey/:question_number', function(req, res) {
//question code
}
Here is the export route code:
exports.survey = function(req, res) {
//Question logic to pass to the render
res.render('question', {
info : info
});
}
For the html, it simply uses the passed "info" to generate the questions and answers and then uses a standard form method=post to send the answer selected back to the app.post.
Can anyone recommend a good method to accomplish this that is not overly complex? Thanks!
You should probably use sessions for this task. After the first question is requested you set a session variable, that will indicate the time, when all questions must be answered. After that you just compare this variable value with a current time of a request. I guess it's one of the most simplest approaches to solve your task.

Resources