My Node.js application sends a response to load the following url in an iframe
http://localhost:3000/fileloadsuccess.html?fname=abc.txt
I get the error
EONET : no such file or directory exists
I have set the routing as
app.get('/fileloadsuccess.html?fname=:fid', function(req,res){
res.sendfile(__dirname + '/fileloadsuccess.html?fname=:fid');
});
What could be the issue?
You can not use query string parameters with res.sendFile().
You also could try so serve fileloadsuccess.html as static file:
https://expressjs.com/en/starter/static-files.html
Or continue to use res.sendFile() like:
res.sendFile('fileloadsuccess.html', { root: __dirname });
To get query string parameters you can use req.query
http://expressjs.com/en/api.html#req.query
Related
I am serving a static file(.js) using script tag (due to webpack). So the request url is like http://localhost:8080/bundle.js But I want to add parameter to this request URL like this:
http://localhost:8080/bundle.js/abc
And according to parameter I will modify bundle.js and serve it.
But How can I add the params.
I'm not sure to understand your goal.
But if you use express to serve your bundle.js (as mentioned in the post tag), to use the parameters, you can do something like :
app.get("/bundle.js/:foo?", (req, res) => {
if (req.params.foo === "bar") {
res.sendFile(__dirname + "/src/bundle2.js");
} else {
res.sendFile(__dirname + "/src/bundle.js");
}
});
Here is a codesandbox with this example:
https://codesandbox.io/s/stackoverflow-74429869-7iz7l2?file=/app.js:242-435
Im trying to deploy cors-anywhere on Google Cloud Functions. Im supposed to provide the url after gcp's link.
It looks like this :
https://us-central1-my-project.cloudfunctions.net/my-function/http://dummy.restapiexample.com/api/v1/employees
but it's transformed to :
https://us-central1-my-project.cloudfunctions.net/my-function/http:/dummy.restapiexample.com/api/v1/employees
All the double slashes after the host are transformed to simple ones.
I tried replacing req.url to transform http:/ to http:// but still wont work. Maybe this needs to be fixed in the webserver level.
Here's my function in GCP
var cors_proxy = require('cors-anywhere').createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: [
'cookie',
'cookie2',
],
// See README.md for other options
});
exports.myFunction = (req, res) => {
req.url = req.url.replace('/my-function/', '/'); // Strip '/my-function' from the front of the URL, else the proxy won't work.
return cors_proxy.emit('request', req, res);
};
Anyone tried to deploy this in a serverless function?
You're using req.url which contains a normalized version of the request URL. You'll want to use req.originalUrl which, as the name suggests, retains the original requested URL. See Express docs for more info.
I'm using serve-static, and it works perfectly with, for example, the string '/var/www/html'.
But, when I write
http.createServer(function(req, res){
var serve = serve-static(req.url, ...)
serve(...)}
with the url:
localhost/var/www/html
It returns me :
'can't get /var/www/html'
How can I redirect my request to the root of my serve-static site?
Ok, I just figured out, that, if I want to put the directory in my url,
I just need to set serve-static('/', ...)
and it will automatically add the url directory
you have to concat the root path, which is in the var __dirname
var serve = serve-static(__dirname + req.url, ...)
I am serving out a static url with express 4.0:
app.use('/static-route', express.static('./static'));
And that works great.
However I would like to redirect my users to a url with a query parameter if they hit that route.
ie /static-route -> /static-route?someQueryParam=hello
I would also like to include middleware for that static request. As a concrete example I am using passport and would like to make sure the user is logged in to access that static content.
app.use (and app.get etc . . .) doesn't take two parameters, the first parameter is the route (optional for use), then the rest are all middleware.
app.use('/static-route', function (req, res, next) {
// validation
// redirect
// etc . . .
next();
}, express.static('./static'));
Use global wilcard route[ app.use('/') ] for static content and
Use specific routes [ app.get(/myroute), app.post('/anotherroute')] for dynamic processing using custom logic
//Serves resources from public folder
app.use('/',express.static(__dirname + '/public'));
//Verify the complete directory path - especially slashes
console.log('Static directory '+__dirname + '/public');
app.get('/list', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>'); });
I am writing a website with a single page web app (the rest of the website is just static files which are served). I am trying to write a piece of middleware for express to redirect all requests that follow the pattern 'example.com/app' to 'example.com/app' so that requests such as 'example.com/app/my/specific/page/' will all result in the same page being sent. The key issue with this is that the url in the address bar of the browser must not change so that the javascript app itself can interpret it and display the correct thing.
I could have done something like this:
app.use( '/app', function ( req, res ) {
res.redirect('/app');
});
However, this causes the url of the page to change and a separate HTTP request is assumedly made.
The most obvious alternative solution is to do something like this:
app.use( '/app', function ( req, res ) {
res.sendfile(__dirname + '/public/app/index.html');
});
The issue here is that resources from the page after requests like 'example.com/app/my/specific/page/' will look in the wrong location. For example, if I have an image on the page such as then it will look for example.com/app/my/specific/page/image.jpg. Since no image is returned, it will not display on the page. This happens for all external scripts or stylesheets.
I also tried something like this:
app.use( '/app', function ( req, res ) {
res.sendfile(__dirname + '/public/beta' + url.parse(req.url).pathname);
});
but that was very stupid of me for obvious reasons.
In the end I used this middleware to serve the app's page when appropriate
// all unmatched requests to this path, with no file extension, redirect to the dash page
app.use('/dash', function ( req, res, next ) {
// uri has a forward slash followed any number of any characters except full stops (up until the end of the string)
if (/\/[^.]*$/.test(req.url)) {
res.sendfile(__dirname + '/public/dash/index.html');
} else {
next();
}
});
I then set used a base HTML element with the href attribute pointed to the root.
If you're still trying to accomplish this I may have found a starting point. Alexander Beletsky has a Backbone.js + Express SPA boilerplate repo Located Here.
For a brief article on how it came about you can read his article on Dzone.