Send other domain's file from expressJs - node.js

I have a server (nodeJS, expressJS).
I created a route /abc in which I want to send a HTML file which is stored at Google Cloud Bucket.
I tried using res.sendFile but since it send files from the internal server, so it didn't worked for me.
My code for res.sendFile :-
router.get('/abc', function(req, res) {
res.sendFile("https://storage.googleapis.com/BUCKET_NAME/FILENAME.html");
});
How to achieve this kind of scenario.
Thanks in advance.

use request npm : https://www.npmjs.com/package/request
app.get('/abc', function(req, res) {
req.pipe(request('https://storage.googleapis.com/BUCKET_NAME/FILENAME.html')).pipe(res)
})

Something like this
import http;
var pipeRequest=(url,res)=>
http.get(url, (getRes)=>{
res.setHeader("content-type", getRes.headers['content-type']);
getRes.pipe(res);
}
);
router.get('/abc', function(req, res) {
pipeRequest("https://storage.googleapis.com/BUCKET_NAME/FILENAME.html",res);
});

Related

ExpressJS Render Path with Hash

How can I set a url such as localhost:8080/foo#specialStuffHere from my ExpressJS application? I am using code in my router such as:
app.get('/foo/', function (req, res) {
res.render('foo', {myData: data});
});
Try using the command res.redirect(your url here) instead of res.render().

Express rest api get all endpoints

I have nodejs app with express framework. If I want to catch all endpoints for a http method, say 'GET', how would I define it. I tried :
app.get('*', ... )
But it doesn't seem to work. I don't want to use 'ALL' , just specific method.
As I know you should use:
app.get('/*',.......)
try using for get method
app.get('*', function(req, res) { ... });
or use this for all methods
app.get('*', function(req, res) { ... });

How do I use app.get to route to a new page on Nodejs and express

I've been trying to use app.get to route to a new html page like so:
e.g. when I type localhost:3000/newpage i want it to route to newpage.html
I've only been able to route to another JS file through app.get. Is it possible to do this but instead route to a html file? If not, is there a more appropriate method to doing this? I'm new to nodejs so any help at all would help!
What I have currently
app.js
var graball = require('./public/javascripts/graball')
app.get('/graball', function (req, res) {
res.send(ibm);
});
What I want
app.js
var page = require('./public/page1'); //page1.html
app.get('/page1', function(req, res) {
res.send(page1);
}
You can use sendFile:
app.get('/sitemap',function(req,res){
res.sendFile('/sitemap.html');
});

In express how do I redirect a user to an external url?

I have a payment system using node.js and braintree, when the payment is successful I want to send the user to the back end. My back end is setup elsewhere.
I have tried
res.writeHead(301,
{Location: 'http://app.example.io'}
);
res.end();
So window.location is obviously not available. I cant think of any ways to redirect a user?
You can do
res.redirect('https://app.example.io');
Express docs: https://expressjs.com/en/api.html#res.redirect
The selected answer did not work for me. It was redirecting me to: locahost:8080/www.google.com - which is nonsense.
301 Moved Permanently needs to be included with res.status(301) as seen below.
app.get("/where", (req, res) => {
res.status(301).redirect("https://www.google.com")
})
You are in the same situation since your back-end is elsewhere.
app.get("/where", (req, res) => {
res.status(301).redirect("https://www.google.com")
})
You need to include the status (301)
I just have the same issue and got it work by adding "next". I use routers so maybe you have same issue as mine? Without next, i got error about no render engine...weird
var express = require('express');
var router = express.Router();
var debug = require('debug')('node_blog:server');
/* GET home page. */
router.get('/', function(req, res, next) {
debug("index debug");
res.render('index.html', { title: 'Express' });
});
router.post("/", function (req, res, next) {
//var pass = req.body("password");
//var loginx = req.body("login");
//res.render('index.html', { title: 'Express' });
res.redirect("/users")
next
});
module.exports = router;
None of these worked for me, so I tricked the receiving client with the following result:
res.status(200).send('<script>window.location.href="https://your external ref"</script>');
Some will say if noscript is on this does not work, but really which site does not use it.

Angular Service to Express API Route + Posting Data

I am in the process of converting one of my sites (http://maskedarmory.com) from LAMP (using Laravel 4 MVC) over to the MEAN stack and it has been quite a journey thus far.
I have managed to get the landing page up and running and the input POSTing to Angular controller that I have it routed to.
Now, the problem I am having is getting the service to send over the POSTed data that is in Angular over to the Express API. I keep keeping a 404 Not Found error on the /api/character URL path.
Also, how do I access the 'characterData' variable that is on the Angular side that is being passed over by the factory? Because I am trying to do a console.log on the 'characterData' variable on the server side and I am sure that that is out of scope.
app/routes.js (Express Routing)
// public/js/services/ArmoryService.js
angular.module('ArmoryService', []).
factory('Armory', function($http) {
return {
// Get the specified character by its profile ID.
get : function(id) {
return $http.get('/api/character/' + id);
},
// call to POST and create a new character armory.
create : function(characterData) {
return $http.post('/api/character', characterData);
}
}
});
app/routes.js (Express Routing)
module.exports = function(app) {
app.post('/api/character', function(req, res) {
console.log(characterData);
});
app.get('/', function(req, res) {
res.sendfile('./public/index.html'); // load our public/index.html file
});
};
If I do a console.log before the $http.post to the API, 'characterData' has all of the information it should.
I am sure that this is a routing issue of some type, but I will be damned if I can figure it out.
Thanks in advance for your help!
Try this:
app.post('/api/character', function(req, res) {
console.log(JSON.stringify(req.body));
res.status(200).send('whatever you want to send back to angular side');
});
app.get('/api/character/:id', function(req, res) {
console.log(req.params.id);
res.status(200).send('whatever you want to send back to angular side'');
});

Resources