Problems with getRoutingService().calculateRoute parameters - here-maps-rest

I am looking into the HereMaps as a possible map provider for our company and am having difficulty with the return params for calculateRoute. I am trying to get the route instructions to be returned from the endpoint but it fails when trying to do so. I am currently passing in ...
var routingParameters = {
routingMode: 'fast',
transportMode: 'truck',
// The start point of the route:
origin: position.coords.latitude + ',' + position.coords.longitude,
// The end point of the route:
destination: '50.672770,-120.375370',
return: 'polyline',
};
This seems to work however when i try to do return: 'polyline, actions, instructions', to get the instruction info the route request fails. I noticed in the docs that these are available but don't seem to work for my case. My guess is it may be due to being on the free version for now. Any help is appreciated.

Related

Express: defining REST API with sub-resources?

I implemented a REST API via Express.
I got a list of suppliers, and for each one list of inventory.
the URL went:
/Supplier/Inventory{supplierId} (e.g http://MyServer/Supplier/Inventory/237 to get inventory list of supplier 237)
So... pretty standard stuff: in my server.js I use: (and I simply here but you guys get the idea)
const routeInventory = require('./routes/Inventory');
...
app.use('/api/Supplier/Inventory', routeInventory);
and in routes file:
outer.route('//:id').get(ShowInventory);
and the function would be something like:
exports. ShowInventory= (req, res, next) => {
const iSupplierId=req.params.id;
console.log('ShowInventory for SupplierId:' + iSupplierId);
...
}
very simple. req.params.id gives me the param I need from the URL.
So far so good. But! somebody here insists that since Inventory is a sub-entity of supplier, the URL should be:
/Supplier/{supplierId}/Inventory
so two questions:
Are they correct? is that the standard?
no matter how much I play with the express code, I can't get the {supplierId} parameter when its in the middle of the URL... how is that done?
Thanks Much
Yes, if you request subresources of a certain resource then you should indicate an id of a resource and only after that indicate subresource name:
/supplier/{supplierId}/inventory
As of routing this can be achived like this:
router.get('/supplier/:supplierId/inventory', ShowInventory)
that way you can access supplierId like this:
const supplierId = req.params.supplierId

Node.js - Why does my HTTP GET Request return a 404 when I know the data is there # the URL I am using

I'm still new enough with Node that HTTP requests trip me up. I have checked all the answers to similar questions but none seem to address my issue.
I have been dealt a hand in the Wild of having to go after JSON files in an API. I then parse those JSON files to separate them out into rows that populate a SQL database. The API has one JSON file with an ID of 'keys.json' that looks like this:
{
"keys":["5sM5YLnnNMN_1540338527220.json","5sM5YLnnNMN_1540389571029.json","6tN6ZMooONO_1540389269289.json"]
}
Each array element in the keys property holds the value of one of the JSON data files in the API.
I am having problems getting either type of file returned to me, but I figure if I can learn what is wrong with the way I am trying to get 'keys.json', I can leverage that knowledge to get the individual JSON data files represented in the keys array.
I am using the npm modules 'request' and 'request-promise-native' as follows:
const request = require('request');
const rp = require('request-promise-native');
My URL is constructed with the following elements, as follows (I have used the ... to keep my client anonymous, but other than that it is a direct copy:
let baseURL = 'http://localhost:3000/Users/doug5solas/sandbox/.../server/.quizzes/'; // this is the development value only
let keysID = 'keys.json';
Clearly the localhost aspect will have to go away when we deploy but I am just testing now.
Here is my HTTP call:
let options = {
method: 'GET',
uri: baseURL + keysID,
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
rp(options)
.then(function (res) {
jsonKeysList = res.keys;
console.log('Fetched', jsonKeysList);
})
.catch(function (err) {
// API call failed
let errMessage = err.options.uri + ' ' + err.statusCode + ' Not Found';
console.log(errMessage);
return errMessage;
});
Here is my console output:
http://localhost:3000/Users/doug5solas/sandbox/.../server/.quizzes/keys.json 404 Not Found
It is clear to me that the .catch() clause is being taken and not the .then() clause. But I do not know why that is because the data is there at that spot. I know it is because I placed it there manually.
Thanks to #Kevin B for the tip regarding serving of static files. I revamped the logic using express.static and served the file using that capability and everything worked as expected.

Accessing response headers using NodeJS

I'm having a problem right now which I can't seem to find a solution to.
I'm using Uservoice's NodeJS framework to send some requests to UserVoice regarding Feedback posts. A problem I've run into are ratelimits so I want to save the header values X-Rate-Limit-Remaining, X-Rate-Limit-Limit and X-Rate-Limit-Reset locally. I've made a function for updating and getting that value and am calling it like this:
var content = "Test"
c.post(`forums/${config.uservoice.forumId}/suggestions/${id}/comments.json`, {
comment: {
text: content
}
}).then(data => {
rl.updateRL(data.headers['X-Rate-Limit-Limit'],data.headers['X-Rate-Limit-Remaining'],data.headers['X-Rate-Limit-Reset'])
When running this code I get the error Cannot read property 'X-Rate-Limit-Limit' of undefined.
This is not a duplicate, I've also tried it lowercase as described here but had no luck either. Thanks for helping out!
EDIT:
The function takes the following parameters:
module.exports = {
updateRL: (lim, rem, res) {SAVING STUFF HERE}
}
It is defined in the file rates.jsand is imported in the above file as const rl = require('../rates').

How to identify request (by ID) through middleware chain in Express.

I am developping a RESTful server in node.js, using Express as framework, and Winston, for the moment, as logger module.
This server will handle a big amount of simultaneous request, and it would be very useful to me to be able to track the log entries for each specific request, using something like a 'request ID'. The straight solution is just to add this ID as another piece of logging information each time I want to make a log entry, but it will mean to pass the 'request ID' to each method used by the server.
I would like to know if there is any node.js/javascript module or technique that would allow me to do this in an easier way, without carrying around the request ID for each specific request.
If you auto-increment, your later log analytics won't be able to uniquely identify requests, because different instances will generate colliding IDs, and restarting the app will automatically cause ID collisions.
Here's another possible solution.
Install cuid:
npm install --save cuid
Then in your main app file:
var cuid = require('cuid');
var requestId = function requestId(req, res, next) {
req.requestId = cuid();
next();
};
// Then, at the top of your middleware:
app.use(requestId);
Now you'll get a friendly request ID that is unlikely to collide, and you'll be able to uniquely identify your requests for your log analytics and debugging, even across multiple instances, and server restarts.
You can use req object that does comes with every request in express.
So the first route you would do in your application would be:
var logIdIterator = 0;
app.all('*', function(req, res, next) {
req.log = {
id: ++logIdIterator
}
return next();
});
And then anywhere within express, you can access that id in req object: req.log.id;
You will still need to pass some data into functions that do want to create some logs. In fact you might have logging function within req.log object, so that way it will be guaranteed that logging will happen only when there is access to req.log object.
I was struggling search for a solution for this problem.
The thing I didn't like it about solutions suggested here was that they imply to share the req object among all the functions along the project.
I found out a solution mixing your approach (creating an uuid per request) and with a library (continuation-local-storage) that allows sharing namespaces among modules.
You can find the explanation in this other answer: https://stackoverflow.com/a/47261545/5710581
If you want more info, I wrote down all these ideas and all the code in a post, in order to explain everything in one place:
Express.js: Logging info with global unique request ID – Node.js
You shouldn't be using Global Variables.
What I like to do is to populate a META object before each request.
I use a UUID generator (https://github.com/kelektiv/node-uuid) to ID a request
Here's an example
app.all('*', function(req, res, next) {
req.meta = {
ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
timestamp: uuid(),
user_agent: req.headers['user-agent'],
body: req.body,
}
return next();
})
As mentioned by #moka , Using the request ID in each request is the crux of solving the problem. Another way of abstracting all these is by making use of http-context and uuid
So set a UUID in the httpContext before all your middlewares (set as an application middleware and not as a router middlware). now you can get the uuid anywhere in your code and log it.
Here is a sample implementation I have used
You can get the complete reference here uuid in request
const uuid = require('node-uuid');
const httpContext = require('express-http-context');
....
this.expressApp.use(httpContext.middleware);
this.expressApp.use((req, res, next) => {
httpContext.set('reqId', uuid.v4());
next();
});
Now I have used the reqId set here in my custom pino logger'
public infoLogService (fileName): pino.Logger {
return pino({
level: 'info',
name: this.appService.getApp_name(),
messageKey: 'XXX-Logs',
base: {pid: process.pid, hostname: os.hostname,
timestamp: this.getTimeStamp(),
appName: this.appService.getApp_name(),
fileName: fileName,
request_id: **isNullOrUndefined(httpContext.get('reqId'))** ? 'Not an actual request ' : httpContext.get('reqId')
},
enabled: true,
useLevelLabels: true,
});
}
If the reqId is null it means that the loggers have been inserted in code that is used before starting the express App. Hope you can use this as an alternate solution

Node.js Superagent is not populating results consistently

I have this route that queries an API to get information about the project. It uses super agent to post a get request, passes along some headers with the .set and project_results should contain the data.
Now here is the issue: I can load the page for this particular route/project 20 times... 19 of the times it will work perfectly but randomly it will throw an error:
TypeError: Cannot read property 'creator' of null
This error points out the line: with if(project.creator == req.signedCookies.user_3rb._id) {
So I know it is making it past the: if(project_results.status == '200') {
and since I am looking at the same project over and over and over (and I know all projects have a creator I checked the DB)
my question would be why sometimes does it not find this property of the project_results variable? Its very inconsistent.. I would think project_results is completely populated before going through the code... since its passing the status check we know there is at least some data in the project_results variable..
app.get('/user/projects/:project_id', function(req, res, next) {
var agent = superagent.agent();
var project = {};
project.id = req.params.project_id;
agent
.get(apihost + '/api/project/'+project.id)
.set('api_key', apikey)
.set('access_token', user.access_token)
.end(function(project_error, project_results) {
if (project_error) {
console.log(project_error);
}
if(project_results.status == '200') {
project = JSON.parse(project_results.text);
// Check if we are the owner of the project
if(project.creator == req.signedCookies.user_3rb._id) {
project.owner = true;
}
......
I'm one of the contributors to superagent.
I'd recommend isolating a test case instead of trying to debug directly in your app.
One simple way is to create a very short express app that always returns a hardcoded, perfect response to the route you're trying to reach - then see if project is still null 1 in 20 times:
var app = express();
app.use(function(req, res, next) {
res.send({
// your data hardcoded here
});
});
app.listen(3000);
If it's still null 1 in 20 times, then please submit it as an issue and I'll check it out. If not, then likely something else is going on - perhaps the data isn't being consistently fetched from the db, or there's a race condition between the fetch and the rendering.

Resources