I tried following these instruction but couldn't get the URI decoded. how can I go about this?
When I enter a city like http://localhost:5000/weather?weatherCity=Malmö the URL changes to this http://localhost:5000/weather?weatherCity=Malm%C3%B6,
How can I decode the last part and what am I doing wrong?
app.get('/weather', (req, res) => {
const weatherCity = (req.query.weatherCity)
let decodeURI = decodeURIComponent(weatherCity) //<------- trying to decode the query
request(weatherURL(decodeURI), function (error, response, body) {
if (error) {
throw error
}
const data = JSON.parse(body)
return res.send(data)
});
})
function weatherURL(weatherCity){
return `https://api.openweathermap.org/data/2.5/weather?q=${weatherCity}&units=metric&appid=${process.env.APIKEY}&lang=en`
}
This is probably what you need:
app.get('/weather', (req, res) => {
const weatherCity = req.query.weatherCity;
request(weatherURL(weatherCity), function (error, response, body) {
if (error) {
throw error
}
const data = JSON.parse(body)
return res.send(data)
});
})
function weatherURL(weatherCity){
return `https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(weatherCity)}&units=metric&appid=${process.env.APIKEY}&lang=en`
}
There should be no need to decode req.query.weatherCity because express does this automatically.
You do need to encode weatherCity before building a URL with it. URL query parameters should be URL encoded.
Consider using something other than request because it is deprecated and doesn't support promises. node-fetch and axios, among others, are good choices.
Related
I am trying to call an external REST API (dynamical URL) through a parameter in express and turn that into an API for my site(most of these APIs require being run on the web server, and won't run on the client). However, I'm not sure how exactly to go about combining this? I've been searching around on SO, but most solutions come to no success for me. I'm trying basically to:
Retrieve link from front end -> use the link to get info from API -> to use my own /getInfo API to send the data I retrieved back.
Snippet:
app.get('/api/getInfo/', (req,res) => {
const info = req.query.url;
res.send(getRest(info));
})
function getRest(url) {
Request.get(url, (error, response, body) => {
console.log(JSON.parse(body));
})
}
I'm calling it on my frontend via this method:
const GET_INFO_API = "http://localhost:3001/api/getInfo"
...
getInfo(url) {
return axios.get(GET_STEAM_API, {
params: {
url: url
}
});
}
You should be able to do it by adding a callback to the getRest function:
app.get('/api/getInfo/', (req,res) => {
const info = req.query.url;
getRest(info, (error, response, body) => {
res.send(JSON.parse(body));
});
})
function getRest(url, callback) {
Request.get(url, (error, response, body) => {
callback(error, respose, body);
})
}
The Problem
I deployed a create-react-app webapp to aws ec2. It's used to display data from a database and send data to it. I use ExpressJS, CORS and MySQL.
With the following code i fetch the corresponding URL and the server.js sends back the database content. Until here, everything works fine.
getBets = _ => {
fetch("http://ec2***.amazonaws.com
.then(response => response.json())
.then(response => this.setState({bets: response.data}))
.catch(err => console.error(err))
};
The problem begins when sending data to the database with the following code:
addBet = _ => {
const { bet } = this.state;
fetch(`http://ec2***.amazonaws.com/bets/add?name=${bet.person_name}&bet=${bet.time_bet}`)
.then(response => response.json())
.then(this.getBets)
.catch(err => console.error(err))
};
On click the addBet-function populates the db, but in chrome I following error:
GET http://ec2***.amazonaws.com/bets/add?name=Peter%20Pan5&bet=10:17%205 net::ERR_EMPTY_RESPONSE
and
TypeError: Failed to fetch
Regarding chrome dev-tools, the first error corresponds to the fetch in the addBet function and the second error to the catch part.
On the server side I've the following code for processing the fetch:
app.get("/bets/add", (req, res) => {
const {name, bet} = req.query;
const INSERT_BET = `INSERT INTO bets (name, bet, timestamp) VALUES("${name}", "${bet}", CURTIME())`;
connection.query(INSERT_BET, (err, res) => {
if (err) {
return res.send(err);
}
else {
return res.send("succesfully added your bet");
}
})
});
I want to mention, that the res paramter in the app.get part is unused. That tells me my IDE.
After a lot of hours digging deeper in the topics of expressJS and the fetch api, I guess, that the app.get part doesn't send a response to the server. But the fetch need some response.
My Question
How do I have to change the code in the app.get part to send a proper response back to the server?
AND
Am I right with my guess?
In MYSQL when you do an insert query you get back err,results and fields in the callback function like this:
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function (error,
results, fields) {
if (error) throw error;
console.log(results.insertId);
});
You have used the parameter res for result and then you have used res.send() which now corresponds to that res parameter in the callback function and not the res object.Rewrite it like this:
app.get("/bets/add", (req, res) => {
const {name, bet} = req.query;
const INSERT_BET = `INSERT INTO bets (name, bet, timestamp) VALUES(?,?,?)`;
connection.query(INSERT_BET,[name,bet,CURTIME()] ,(err, result) => {
if (err) {
return res.send(err);
}
else {
return res.send("succesfully added your bet");
}
})
});
I have also used prepared statement in place of normal sql queries. These are used to prevent sql injections. I hope it will work now.
I'm using the request library in Node.js to do a https request to get data from another service. This is called asynchronously, right? So my code keeps running before all of the data is there, correct?
My problem is that the data is needed right afterwards to calculate some things. My code throws an error during that calculation because the data from the service is undefined...
Could it be possible that the data is just not there yet? And if so, what do you do against that?
Here is a copy of the request:
const request = require('request');
request(someUrl, {"Accept": "application/json"}, (err, res, body) => {
if (err)
handleError(err);
body = JSON.parse(body);
return body;
});
This kind of situation is pretty common in react/angular/vue kinda web apps, sometimes you need the data right away. But it is not available then, after a Rest call or something it becomes available.
So, the simplest solution?
Just add a check, for example:
const calculate = (someVal)=>{
if(!someVal) return ;
//otherwise do the calculation
}
There are plenty of other ways, by mostly making the calculation async. For your function, you can do this
const promOp = function(){
return new Promise((resolve, reject) => {
request(someUrl, {"Accept": "application/json"}, (err, res, body) => {
if (err) reject(err);
body = JSON.parse(body);
resolve(body);
});
}
}
//then
promOp()
.then((body)=>{
//calculate here
})
//or can use the `Async/Await` syntax instead of then
const op = async () => {
const body = await promOp;
//calculate here
}
I use expressjs to upload large arraybuffer by POST and content-type is application/octet-stream.
I use middleware to read body content like that:
const getRawBody = require('raw-body');
/**
* Read body have content type is application/octet-stream
*/
app.use(function (req, res, next) {
if (req.headers['content-type'] === 'application/octet-stream') {
// using rawbody to read arrray buffer
getRawBody(req, {
length: req.headers['content-length'],
encoding: this.charset
}, function (err, string) {
if (err)
return next(err)
req.body = string
next()
})
}
else {
next()
}
});
When arraybuffer have large file(> 50MB). It's cant not read the body and return error to client(Chrome crashed, firefox xhr return error).
I don't know why. Please help me resolve this problem.
Many Thanks
Try following code snippet if it works.
app.use(express.bodyParser({limit: '100mb'}));
I have this code
app.get('/imgs/:id', function(req, res) {
// Validate that req.params.id is 16 bytes hex string
// Get the stored image type for this image
var stream = fs.createReadStream(path.join(UPLOAD_PATH, req.params.id));
stream.on("readable", function() {
res.setHeader('Content-Type', "image/jpeg")
stream.pipe(res)
})
stream.on('error', (e) => {
res.redirect(404, "404")
})
});
Now the problem is that I always get an error of
Error: Can't set headers after they are sent.
because I used the res.setHeader function.
However, i don't know how to solve it. Let's say I want to use in a page, that has obviously the res.send() function has well,
the <img src="imgs/pic">, then I must set the header for the this page request to "image/jpeg" because otherwise the browser wouldn't know it's an image and won't show it as one.
What can I do then?
Check Express response document here. Try this code
app.get('/imgs/:id', function (req, res) {
res.sendFile(req.params.id, {root: UPLOAD_PATH, headers: {'Content-Type': 'image/jpeg'}}, function (err) {
if(err) throw err;
else console.log('sent')
})
})