Nested route in node / express js - node.js

I attempting to configure a nested route in Express application like so:
app.put('api/template/:id/page/:pageID', updateTemplatePage);
But when my page makes the call, I get a 404 back. My log shows this:
PUT /api/template/519537192e20b47409c46e72/page/home 404 4ms
home is my page ID in this case, so the call URL looks valid to me. Simpler calls, like GET /api/template/519537192e20b47409c46e72 work just fine. How can I make this work?

I was missing '/' in front of the path. Should be like this:
app.put('/api/template/:id/page/:pageID', updateTemplatePage);
Thank you all for your suggestions.

Related

How to prevent React-router from messing up your API url

I have a POST endpoint set up like so:
// from server.js
const photo = require('./routes/imagesRoute');
app.use("/api/photo",photo);
//imagesRoute.js
router.route('/photos/:id') // final path will thus be api/photo/photos/:id
.post(employeePhotos)
On the frontend, the component from which the request is made has the following path :
http://localhost:3000/employeePhoto/5fc8a739a89f461274a6286f
I am using axios to POST the request like so:
axios.post(`api/photo/photos/${this.state._id}`,formData )
The challenge is that whenever i make a request to the endpoint 'api/photo/photos' i get a 404 response like so :
POST /employeePhoto/api/photo/photos/5fc8a739a89f461274a6286f 404 22.239 ms - 195
obviously this will not match the endpoint url hence the 404! How would i fix this mix up?
It happens beacause your url in axios is relative, you use this:
axios.post(`api/photo/photos/${this.state._id}`,formData )
I would suggest tu put a "/" before the url:
axios.post(`/api/photo/photos/${this.state._id}`,formData )
When not starting the url with /, it will be relative to whatever your path is currently. I solved the same issue based in this concept, more information
StackOverflow ReactJs...

Node/Express - How to change default root URL

I have a single page application I am working on. The goal is to have the main root route be something other than www.website.com. Something like www.website.com/cool-page This is for SEO purposes. I am not sure if it is best practice or even possible.
Thus far I've been able to change the URL on load using this:
// root route
router.get('*', function(req, res) {
res.redirect('/cool-page');
});
This gives me an error that the page redirected you too many times.
Any advice on this?
Your routing definition looks like it would redirect every request to URLs within the root URL, not just those going to the root URL itself. So it will redirect even the redirected requests going to /cool-page, forming an infinite loop. Try using router.get('/', ... ) instead, I think that would redirect only requests made to the root url,

Call NodeJS function from client-side

I have NodeJS on / path.
On /another.ejs path, I have a little website and I wanna get data from /value path.
I cannot do this call with pure JS and AJAX, because of CORS.
Can I do something like when I click on button, it calls function in NodeJS and return data?
I don't know why CORS is going on in same domain name, but you can try some other ways to get result from routes.
Using proxy to throw result between server and client.
You can use proxy things.
Means creating middle hand (link to another stackoverflow answer)
Also look: PHP: no.php
CORS Module
Also see above comment by codeherk.

Unable to access ExpressJS route via href link

I'm not sure if this is a security feature or if I'm missing something obvious. I need to access one of my ExpressJS routes directly via a standard link on the page.
If I type the URL in to my browser location bar, I get the desired result. However, if I put that exact URL in a standard link on one of the pages on the site, the route never gets hit.
EDIT: Even if I pull this out of the router and add directly to app.js I get the same results. I'm simply trying to use Passport's Facebook authentication. The first route looks like this:
router.get('/login/facebook', function(req, res, next) {
passport.authenticate('facebook')(req, res, next);
});
I realize the req function wrapper is not needed, I was using it for debugging so I could see when the route gets called.
If I set a breakpoint there, it works fine if I just type the "/api/login/facebook" URL into my browser, but if I put the exact URL in a link on the page, the route never gets hit.
It turns out this was caused by Angular's routing mechanism intercepting the links. I found the solution here:
How to get Angular UI Router to respect "non-routed" URLs

How does Express handle routes, and what does the '#' do?

I've setup an Express server using backbone.js with a couple routes, and I'm trying to capture information through the url using req.params.
I've setup my server with appropriate routing
app.get( '/route/:first/:second', router.routeHandler );
With my express server, when I type in a url like this:
http://localhost:3000/route/firstVar/secondVar
I get raw JSON returned to me, but when I try a url like this:
http://localhost:3000/#route/firstVar/secondVar
it will actually render the html and CSS to the page. What is going on there? Can I change that behavior? Where is that setup?
Nothing after the hashmark is making its way to the server. If you want to be able to handle that second URL, you'll need to set up the proper routes on the client-side (in your case, using Backbone). Have a peek at Backbone's History and Router documentation for some more information.

Resources