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().
Related
I am trying to get the url parameter of an url request from the front in my nodejs backend.
fetch(`http://localhost:9000/sent/5768797675645657`)
Here is my app.js file :
app.use('/sent/:id', require('./routes/sent'));
And my /routes/sent.js file :
router.get('/', function(req, res) {
console.log(req.params)
})
How can I get the console.log(req.params) to work?
So, app.use accepts two parameters: the portion of the URL, and a callback function.
In order to your app works, you've to change the /routes/sent.js and make it exports the function, so it can be used when you're requiring it.
Just change it to
module.exports = function (req, res) {
console.log(req.params)
}
and you're ready to go!
I am working on to get route value in Nodes js using express framework
the url goes like
http://localhost:3000/course/view/turbine/turcV39/%20V42/%20V44/%20V47
Need to get the value "V39/%20V42/%20V44/%20V47" from the above url and route
router.get('/view/turbine/:turc?', function(req, res) {
console.log('a');
});
You can use regex to get it, like this
app.get(/\/view\/turbine\/turc(.*)/, function(req, res) {
console.log(req.params[0])
});
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);
});
I am using express and jade in my nodejs application. On each route when rendering I need to access same object with config and localization object. It's working fine like this:
loadLoginPage: function(req, res) {
res.render('login', {
config: config,
i18n: res
});
}
But is there a way without rewriting {config:config,i18n:res} on each route? I tried to find how to solve this but without results.
You can use the app.locals object as the following (http://expressjs.com/en/api.html#app.locals) :
app.locals.config = "your config";
app.locals.localisation= "your localisation";
And then
loadLoginPage: function(req, res) {
res.render('login');
}
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');
});