I have question about route to get both request from param and body
My route is to delete user. It looks like this:
router.delete("/delete/:id",middleware, async (req, res) => {
//firstly, I get param:
var userId = req.params.id || '';
//if emty, it will get request from body
if(!userId){
const listId = req.userIds
}
});
I perform request but it shows error: Cannot DELETE /api/users/delete
http://localhost:5000/api/users/delete/
Can you explain me what wrong with my issue?
Based on your latest comment you will need a route for collection delete as well as the model route. Here is some "pseudocode":
// model form
router.delete("/delete/:id",middleware, async (req, res) => {
var userId = req.params.id
// made up backend service - add error handling, etc
await dataService.users.delete(userId);
res.sendStatus(200); // again with error stuff
});
// collection form
router.delete("/delete",middleware, async (req, res) => {
var userIds = req.body.userIds; // assumes use of bodyParser
for (userId in userIds) {
// made up backend service - add error handling, etc
await dataService.users.delete(userId);
res.sendStatus(200); // again with error stuff
}
});
Related
I've trying to create a custom logger middleware without any package or library
It's as simple as saving the endpoint, the method and the status code response.
I have the problem when I try to save the status code, since my response has not yet reached the controller. I was trying to understand how morgan does it, because it is the first middleware I use and when my backend responds, it logs the status code.
Is there a simple way without me having to modify all my backend controllers?
Or rather, how can I access the res.status of a controller from this middleware?
const createLog = (req, res, next) => {
const { method, url } = req;
const { statusCode, statusMessage } = res;
console.log(statusCode, statusMessage); // Both null when reach the middleware
next();
};
Try this here:
const createLog = (req, res, next) => {
res.on("finish", function() {
console.log(req.method, decodeURI(req.url), res.statusCode, res.statusMessage);
});
next();
};
The existing code was written as MySQL query and I am now working on converting it to Mongoose query.
I need to get five data sorted by the most recent subscription year from the main page.
The existing code brought this result value into an array. And data was delivered through pug view, and Mongoose seems to bring the result value of Object. In this case, I wonder how to deliver the data through Pug view.
I checked importing data from the terminal to the console.log, but an error called 'Error [ERR_HTTP_HEADERS_SENT]: Cannot set heads after they are sent to the client occurs and no data is passed to the pug. I wonder why this problem occurs.
[MySQL Query]
router.get("/", function (req, res, next) {
// Main page Profile Data Process
db.query(`SELECT * FROM user ORDER BY registerDate DESC LIMIT 5`, function (
error,
data
) {
// Log Error
if (error) {
console.log(error);
}
res.render("main", {
dataarray: data,
_user: req.user,
url: url
});
});
});
[Mongoose Query]
router.get("/", function (req, res, next) {
let dataarray = [];
let userData = db.collection("user").find().limit(5).sort({
"created_at": -1
});
userData.each(function (err, doc) {
if (err) {
console.log(err);
} else {
if (doc != null) {
dataarray.push(doc)
}
}
// console.log(dataarray.login)
console.log(dataarray);
res.render("main", {
dataarray,
_user: req.user
})
});
});
[pug file]
each profile in dataarray
.col-lg-4
img.rounded-circle(src=`${profile.avatar_url}` alt='Generic placeholder image' width='140' height='140')
h2=`${profile.login}`
p=`${profile.bio}`
p
a.btn.btn-secondary(href=`/${profile.login}` role='button') View details ยป
You are sending the request in multiple chunks, node/express uses one request and one response.
Cannot set heads after they are sent to the client
Is the error that happens when the res.render is called the second time. At this point, the one request has already left the node/express process and this is tell you that you're trying to violate the one request/one response paradigm.
This is the part of your code where you can see why this happens.
router.get("/", function (req, res, next) {
let dataarray = [];
let userData = db.collection("user").find().limit(5).sort({
"created_at": -1
});
userData.each(function (err, doc) {
This part of your code will try to send a response for each item in your resultset.
Something like this will work properly (I didn't test it):
router.get("/", function (req, res, next) {
db.collection("user").find().limit(5).sort({ "created_at": -1 }, function(err, userData){
res.render("main", {
dataarray: userData,
_user: req.user
})
});
});
In other words, only one res.render is required and pass the entire result set into that.
I'm trying to set up an endpoint like below:
http://localhost:5000/guardian/lifeandstyle/2020/apr/26/bring-your-skin-to-life-with-a-hint-of-bronzer
I currently set up my endpoint like this:
router.get('/guardian/:articleId', (req, res) => {
const id = req.params.articleId;
console.log(id);
axios.get('https://content.guardianapis.com/'+ id +'?api-key=' + guardianapi + '&show-blocks=all')
.then(function (response) {
res.send(response.data);
});
});
But I'm getting a 404 error once I enter the endpoint in my browser
See the guide which has an example:
app.get('/users/:userId/books/:bookId', function (req, res) {
res.send(req.params)
})
You just put multiple / in the route and multiple names starting with a :.
what command I should write in ExpressJS file just so that exposes a single HTTP endpoint (/api/search?symbol=$symbol&period=$period)
Working
app.get('/api/search/', (req, res) => {
res.send(req.query)
})
Not working:
app.get('/api/search?symbol=$symbol&period=$period', (req, res) => {
res.send(req.query)
})
app.get('/api/search?symbol=$symbol&period=$period', (req, res) => {
res.send(req.query)
})
In place of this, you have to write below code
const note = require('../app/controllers/note.controller.js');
// Create a new API CALL
app.get('/comment/get', note.index); // In socket.controller.js i have function with the name of index
//note.controller.js file code
exports.index = (req, res) => {
var requestTime = moment().unix();
req.body = req.query;
console.log(req.body); // you will able to get all parameter of GET request in it.
}
Let me know if i need to explain more about
And for sample code of express for API you can view this...
https://github.com/pawansgi92/node-express-rest-api-sample
What I think you're looking for is this:
app.get('/api/search', (req, res) => {
let symbol = req.query.symbol
let period = req.query.period
})
So when you navigate to /api/search?symbol=foo&period=bar
req.query.symbol is "foo"
and req.query.period is "bar"
I'm making an API call in a POST route but for some reason, I can't pass the JSON data through res.render in the POST route. So I'm thinking about passing the JSON object to GET route so I can render it to the right client page.
Heres my GET and POST routes:
router.get('/bookDetails', (req, res) => {
res.render('bookDetails');
});
router.post('/bookDetails', (req, res) => {
let ID = req.body.ID;
request('https://www.googleapis.com/books/v1/volumes/' + ID, (err, response, body) => {
if(!err && response.statusCode == 200){
let bookdata = JSON.parse(body);
res.render('bookDetails', {bookdata: bookdata});
}else{
console.log(err);
}
});
});
I can't read the bookdata in my bookDetails.ejs file? Is there another way pass this data to the page?
On semantic, it should be a GET router to display something about the ID resource.
router.get('/bookDetails/:id', (req, res) => {
let resource = await fetchResourceById
res.render('bookDetails', resource);
});
also, you can define a middleware function to reuse the fetchResource logic, as following:
function fetchResourceMiddleware(){
return function(req, res, next){
var id = req.query.id || req.body.id
if(id){
req.resource = await fetchResource(id)
}
next()
}
}
reuse the middleware function for GET and POST router:
function renderResource(req, res){
res.render('bookDetails', req.resource);
}
router.get('/bookDetails/:id', fetchResourceMiddleware(), renderResource)
router.post('/bookDetails', fetchResourceMiddleware(), renderResource)
hope helpful, good luck!
After post, your get method will run.
In the get method, you are not sending any data to ejs template, so it will not detect it.
You should redirect in post method, it is bad idea sometimes,