node express get route - node.js

I need to create express route for the following url:
www.somesite.com/some-text-goes-here-id
In this case i want to have to parameters:
text: some-text-goes-here
id: id
Official documentation states the following example:
Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }
However in my case i need to have multiple '-' and only the last one should be id ...
This would be example
Route path: /flights/???
Request URL: http://localhost:3000/flights/some-text-goes-here-123
req.params: { "from": "some-text-goes-here", "to": "123" }
Im not sure if this is even possible to do this way?
Thanks

From docs:
Since the hyphen (-) and the dot (.) are interpreted literally, they
can be used along with route parameters for useful purposes.
So you can write just
Route path: /flights/some-text-goes-here-:id
Request URL: http://localhost:3000/flights/some-text-goes-here-123
req.params: { "id": "123" }

that is not possible. You can either have
/flights/:from/:to
then you can have
req.params: { "from": "some-text-goes-here", "to": "123" }
or have
/flights/:from-to-dest
then you can have
req.params: { "from-to-dest": "some-text-goes-to-123" }
and then split the text by delimiter -to- and take 2nd token
or split just by - and take last token.

I think I found out pretty easy way to do it:
Route path: /site/*-:id
Request URL: http://localhost:3000/site/some-text-goes-here-123
req.params: { "0": "some-text-goes-here", "id": "123" }
And if i just want to handle id below goes another route:
Route path: /site/:id
Request URL: http://localhost:3000/site/123
req.params: { "id": "123" }
Only downside here is that first param is unnamed "0".

Related

How to append HTTP headers and append query parameters in one request using declarativeNetRequest rules?

With Manifest V2, it is possible to have access to the sequence of events in webRequest API.
I can easily block requests, modify headers, append additional query parameters and then redirect to constructed URL.
The outcome is that there is one request that is redirected to the newly constructed URL, which has all modifications (including modified headers and new query parameters).
Now, I'm trying to do the same in Manifest V3 using declarativeNetRequest API. I cannot find any official docs on how could I have the same functionality in MV3.
I have defined two dynamic rules:
addParameterToRequest: {
addRules: [
{
id: 1,
priority: 1,
action: {
type: 'redirect',
redirect: {
transform: {
queryTransform: {
addOrReplaceParams: [{key: data.key, value: data.value}],
},
},
},
},
condition: {
urlFilter: data.urlPattern,
resourceTypes: ['main_frame'],
},
},
],
removeRuleIds: [1],
},
addCookie: {
addRules: [
{
id: 3,
priority: 1,
action: {
type: 'modifyHeaders',
responseHeaders: [
{header: 'Cookie', operation: 'append', value: data.value},
],
},
condition: {
urlFilter: data.urlPattern,
resourceTypes: ['main_frame'],
},
},
]
removeRuleIds: [3],
}
In MV3, the outcome is that I have two requests - one which was redirected with an appended query parameter, and the second one with a modified header.
Manifest V2 results:
Original URL = https://localhost/route
Modified URL = redirected https://localhost/route?qparam=value with appended value in Cookie header
Manifest V3 results:
Original URL = https://localhost/route
Modified URL #1 = redirected https://localhost/route?qparam=value
Modified URL #2 = https://localhost/route with appended value in Cookie header
I tried to somehow combine these two rules into one, but with no success. I tried to play with priority in these rules, but whatever I do, the outcome is that I always have two requests instead of one.
How can I achieve the same functionality in MV3?

Adding a mailbox filter to Gmail

I am trying to add a simple mailbox filter to Gmail, and getting error 400. The response text says that "Filter doesn't have any criteria" but of course I believe I do. Here is the payload data:
{
"filter": [{
"id": "ABC12345-2",
"criteria": {
"from": "donald trump"
},
"action": {
"addLabelIds": ["TRASH"]
}
}]
}
This is the URL that I am posting to:
https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/filters
There is no problem with authentication. I have tried it with and without the "id" field. Any ideas about what I am doing wrong?
I'm not sure about your actual script. So I'm not sure about the requirement of {"filter": []}. But when I saw the official document, it seems that the sample request body for the endpoint of POST https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/filters is as follows, when your request body is used.
Sample request body:
{
"id": "###",
"criteria": {"from": "###"},
"action": {"addLabelIds": ["TRASH"]}
}
id: string, The server assigned ID of the filter.
criteria: object (Criteria), Matching criteria for the filter.
action: object (Action), Action that the filter performs.
In this case, even when id is not used, no error occurs.
You can also test above at "Try this API".
References:
Method: users.settings.filters.create
Resource: Filter

queryStringParameters in mock-server

I have the following url:
http://10.11.100.163:1080/v1/publish/?category=conf&product=UFED_INFIELD&serial=123&productGUIDs%5B0%5D%5Bproduct%5D=GLOBAL&productGUIDs%5B0%5D%5Bguid%5D=undefinedblabla&productGUIDs%5B1%5D%5Bproduct%5D=UFED_INFIELD&productGUIDs%5B1%5D%5Bguid%5D=undefinedblabla
As you can see there are several parameters that are formed by two names, like "productGUIDs%5B0%5D%5Bproduct%5D=GLOBAL" and this is equal to "productGUIDs[0][product]=GLOBAL"
now in the expectation file on the mock-server I am trying to create the request but without success until now.
this is what I wrote in the expectation file:
await mockServerClient(process.env.mockServerAddress , process.env.mockServerPort).setDefaultHeaders(defaultResponseHeaders, []).mockAnyResponse({
"httpRequest":{
"method": "GET",
"path": "/v1/publish",
"queryStringParameters": {
"category":["conf"],
"product":["UFED_INFIELD"],
"serial":["123"],
"productGUIDs%5B0%5D%5Bproduct%5D" : ["GLOBAL"],
"productGUIDs%5B0%5D%5Bguid%5D" : ["undefinedblabla"],
"productGUIDs%5B1%5D%5Bproduct%5D" : ["UFED_INFIELD"],
"productGUIDs%5B1%5D%5Bguid%5D" : ["undefinedblabla"],
}
},
when sending the request (GET) with POSTMAN, I get 404, means, the mock-server does not recognize the request.
any advice of how to write the query string parameters in the expectation file will be really appreaciated
The correct queryStringParameters syntax is an array of objects with name/values keys, like this:
"queryStringParameters": [
{
"name": "category",
"values": ["conf"]
},
{
"name": "product",
"values": ["UFED_INFIELD"]
},
{
"name": "productGUIDs%5B0%5D%5Bproduct%5D",
"values": ["GLOBAL"]
}
]
Here an example of an expectation in a file.yaml for a POST request with queryStringParameters. To adapt it to the GET method just delete the body and change "POST" by "GET" :
times:
remainingTimes: 1
unlimited: false
httpRequest:
method: POST
path: /accounts/login
queryStringParameters:
urlparameter1: 'value1'
body:
type: PARAMETERS
parameters:
username: myusername
password: mypassword
httpResponse:
body: {
"access_token": "e55etg9c-167e-4841-b0r3-y8fe5d1t7568",
"token_type": "bearer",
"redirectUrl": "/menu/clothes"
}
statusCode: 200
reasonPhrase: OK
The indexation is very important in the .yaml file, so be careful to have the good format for each element, otherwise it won't work.
You can find here the documentation to do expectations :
https://www.mock-server.com/mock_server/creating_expectations.html#button_match_request_by_negative_priority
Maybe you'll find your answer in the example "match request by path parameter and query parameter" in the link above.

How to include fields in api server and remove it before returning to results to client in Graphql

I have a Node.js GraphQL server. From the client, I am trying get all the user entries using a query like this:
{
user {
name
entries {
title
body
}
}
}
In the Node.js GraphQL server, however I want to return user entries that are currently valid based on publishDate and expiryDate in the entries object.
For example:
{
"user": "john",
"entries": [
{
"title": "entry1",
"body": "body1",
"publishDate": "2019-02-12",
"expiryDate": "2019-02-13"
},
{
"title": "entry2",
"body": "body2",
"publishDate": "2019-02-13",
"expiryDate": "2019-03-01"
},
{
"title": "entry3",
"body": "body3",
"publishDate": "2020-01-01",
"expiryDate": "2020-01-31"
}
]
}
should return this
{
"user": "john",
"entries": [
{
"title": "entry2",
"body": "body2",
"publishDate": "2019-02-13",
"expiryDate": "2019-03-01"
}
]
}
The entries is fetched via a delegateToSchema call (https://www.apollographql.com/docs/graphql-tools/schema-delegation.html#delegateToSchema) and I don't have an option to pass publishDate and expiryDate as query parameters. Essentially, I need to get the results and then filter them in memory.
The issue I face is that the original query doesn't have publishDate and expiryDate in it to support this. Is there a way to add these fields to delegateToSchema call and then remove them while sending them back to the client?
You are looking for transformResult
Implementation details are:
At delegateToSchema you need to define transforms array.
At Transform you need to define transformResult function for filtering results.
If you have ability to send arguments to remote GraphQL server, then you should use
transformRequest

How do you configure CouchDB vhosts to point to rewrites in design doc

Ok so now I've added a rewrites.js file to my Couchapp root folder. In this js file I have...
[
{
"method": "GET",
"from": "/home",
"to": "app/index.html",
}
]
Now when I push the Couchapp, in CouchDB in my _design/rednecks I see it's creating a "rewrites" property with the above js. So this looks all good to me. This is how to do this right?
I changed my local.ini vhost to...
[vhosts]
rnr.couchdb:5984 = /rednecks/_design/rednecks
In Chrome I enter url...
http://rnr.couchdb:5984/home
I get same error...
{"error":"not_found","reason":"Document is missing attachment"}
That app/index.html attachment is absolutely there. I can see it. The app runs fine if I disable rewrites and use the ugly urls. I've tried every variation of the "to" string that I can think of and all I see is that same error above.
Does anyone know how to do this?!?!?!....
Followup...
Ok following Marek's advice and setting my rewrites.json file to this...
[
{ "from": "home", "to": "app/index.html" },
{ "from": "lib/*", "to": "app/lib/*" },
{ "from": "js/*", "to": "app/js/*" },
{ "from": "css/*", "to": "app/css/*" },
{ "from": "img/*", "to": "app/img/*" }
]
It's almost there. It's starting to come together now thanks to your help :-)
I believe you configured your vhost wrong. It should be:
[vhosts]
rnr.couchdb:5984 = /rednecks/_design/rednecks/_rewrite

Resources