Call NodeJS function from client-side - node.js

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.

Related

Setting up Swagger Ui in Firebase Functions Server

I've developed an API on Firebase Cloud Functions and I want to include a docs path to it.
I'm using swagger and I could successfully test it locally (localhost:PORT/docs) but when I deploy the function to Firebase it's not working, it redirects me to an authorization page.
I think I figured out why this is:
Let's say the name of my Cloud function is cfunc. Then the base url for it is something like https://region-name-project-name.cloudfunctions.net/cfunc. Based on how I included the swagger documentation:
const swaggerDoc = require('./docs/swagger.config.json')
app.use(
'/docs',
allowCors,
swaggerUi.serve,
swaggerUi.setup(swaggerDoc, {
customCssUrl: '/assets/swagger.css',
customSiteTitle: 'My Function Title',
customfavIcon: '/assets/logo.ico',
swaggerOptions: {
supportedSubmitMethods: [] //to disable the "Try it out" button
}
})
)
the docs should be located at https://region-name-project-name.cloudfunctions.net/cfunc/docs. When I try to access that URL, watching "Network" in my browser DevTools, it attempts a GET at that URL with response 304 and then redirects to https://region-name-project-name.cloudfunctions.net/docs and that's what brings up the Google Authentication page, since there's no Cloud Function named "docs" so Google thinks I'm trying to access something else in Firebase Cloud Functions (the same thing happens if I do something like https://region-name-project-name.cloudfunctions.net/tomato)
But I still don't know how to fix this redirect or why it's happening. I tried adding the Cloud Function URL to the host parameter of the swagger.config.json file, and some modifications to CORS, like allowing more Request Methods, adding json as content type, allowing authentication on headers, but nothing seems to be working.
Hope I was clear enought, if not tell me any other info you need (it's one of my first posts here :B)
Found the SOLUTION
After testing a BUNCH of different things, I found out that the redirection was in fact happening always removing one slice of the path after, for example I changed the docs endpoint to '/something/docs' and when accessing the URL that would be https://region-name-project-name.cloudfunctions.net/cfunc/something/docs it redirected to https://region-name-project-name.cloudfunctions.net/cfunc/docs which did not bring up the Google Authentication thing but now wasn't a valid path for my docs so it returned a 'Cannot GET /cfunc/docs'.
For some reason this redirection DOES NOT happen if you add an extra forward slash ('/') at the end of the documentation URL. So, in the first case, where the endpoint for the documentation is only '/docs', accessing the URL https://region-name-project-name.cloudfunctions.net/cfunc/docs/ does it. I do not know why that is, I'm probably posting an Issue on the swagger repo, but if someone has some extra data on why or how to make it work otherwise it would be awesome to hear.
Hope this helps someone else!
EDIT:
Oh and another thing I forgot, it's apparently better if you setup swagger-ui as if you were using express Router, even if you are not (maybe Firebase loads the Cloud Function with something like a router), so instead of app.use('/docs', swagger-ui.serve, swagger-ui.setup(swagger-file)) do app.use('/docs', swagger-ui.serve) and then app.get('/docs', swagger-ui.setup(swagger-file))

Cannot find the file URL using xhr.open('post',url,true) - Status: 404 node js

Trying to Upload an image by XMLHttpRequest(), have issue understanding what is the correct URL to access file via xhr.open(...).
Following this example for server side code and everything..
var xhr = new XMLHttpRequest();
xhr.open('post','../../../../server/routes/saveImage.js',true);
xhr.onload = function() {
if(this.status ==200) {
resolve(this.response);
} else {
reject (this.statusText);
}
};
The Project directory is something like this
Project
client
app
component
product
addproduct.js <-- xhr.open is called from here
server
routes
saveImage.js <-- File being called
Also regarding paths let me know if there is a more convenient way to check the access path or absolute path to use in url.
I think there is a conceptual problem in many levels.
First, When you are XHRing(ajax) an url that means you are accessing the url from CLIENT SIDE. So, Let's say you have an app and HTTP posting or getting an url. Do you have that file from client side? The answer is obviously NO.
let's say you are hosting the app in:
http://localhost/myapps/app
So, When you access ./someFile.txt, ../someFile.txt and ../../someFile.txt you are actually requesting
./someFile.txt-> http://localhost/myapps/app/someFile.txt
../someFile.txt-> http://localhost/myapps/someFile.txt
../../someFile.txt-> http://localhost/someFile.txt
Now, For your problem. You need to host the Server Side upload code somewhere. The example assumes the Server Side code is hosted in, for example, http://localhost/upload and use xhr.open('post','/upload',true);
You need to understand requireing or importing or fs.readFile a file is accessing the path internally. But when you host the app, any client side code like Ajax(XHR) is accessing the url from outside.
The problem indeed was with setting up a server side route, this was mostly clarified, thanks to the answer from #Arkita and comment from #Kasper. But I went ahead to dig for a solution, which may not be very useful to others as this was a dumb question in the first place, but here it goes..
On client side
xhr.open('post','/saveImage/save',true);
on server side if you are using Express.js or other connect based frameworks
app.post('/saveImage/save',(req,res,next)=> {....})
Also the example I linked above may be outdated, this seems more helpful.

All requests via Proxy in Node-Webkit

Is there a way to force Node-webkit to use proxied settings for all requests ? I know there is an API solution using setProxyConfig() but this doesn't work with authenticated proxy, the prompt shows up to login but crash when submitted...
So I tried to use node request module, and it works fine :
var request=require('request');
var proxy = request.defaults({'proxy':'http://login:pwd#proxy:port'});
But now my problem is to tell node-webkit to use this for each request.
Any solutions ?
I'm quite new using node and node-webkit so maybe there is a better way to do that.
Thanks a lot !
You may try
process.env.http_proxy = 'http://login:pwd#proxy:port'
It will work with request lib, also should impact other requests from node-webkit (assets, ajax)
But may be other libraries don't use environment proxy settings. If that doesn't work you can try https://www.npmjs.com/package/global-tunnel
Not sure if this is solved, but i came up with a simple solution:
i wrote a simple wrapper for the request library
https://gist.github.com/jodevsa/7dd9662b8244359fa0d7626ae7c9bd69
all you have to do is ,
Head to $PROJECT_DIR/node_modules/request/
Rename index.js to core.js
Create a new file called index.js
Copy code content from the above gist link to index.js
Change proxyLoc value to you'r preferred proxy
If you decided to disable proxy , just change value of ON variable to false inside index.js
Cheers :D

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.

Is there any reason to put user authentication farther down the chain instead at the middleware level in express?

Is there any reason to put user authentication farther down the chain INSTEAD at the middleware level of express (i.e. app.use(express.express.basicAuth(...) )?
(It seems like there is no good reason to live outside of the middleware.)
Why I'm asking. I've gotten some inherited code where the previous programmer put user.auth in the controller.
So a call from the client follows this path:
client >>
middleware of express >>
api_server (ROUTER passes on requests to proper CONTROLLER) >>
api_server (CONTROLLER gets what it needs from DB) >>
api_server (CONTROLLER sends response with data to VIEW on way back to client) >>
client happy ;D
(Please provide suggestions on how to make this question more precise if it's lacking somewhere. I'm getting up to speed on setting up an entire system.)'
Thanks you.
Unless you are targeting that specific route there is no reason to do this. You can even do url specific auth with global middleware as well. The following code is common in my applications.
app.use('/administrator/*', authMiddleware);

Resources