Object accessible on all routes with jade, express? - node.js

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');
}

Related

nodejs get req.param from route

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!

Dynamically created proxy URL using Node/Express

I want to use express to create unique proxy instances using URLs that I am storing in a database. I found an npm module that may help with this called http-express-proxy but open to other solutions that uses express.
And I had a route like this (using http-express-proxy):
user.URL = 'https://www.google.com'
app.post('/', proxy(user.URL))
// and after this, user.URL is updated to a different value. I want the proxy's address to change too.
I did find a solution that dynamically creates a regular express route during runtime, but I cannot get it to work using the proxy() method from http-express-proxy:
https://alexanderzeitler.com/articles/expressjs-dynamic-runtime-routing/
According to that approach, I can require a 2nd file inside the POST route that looks like this: (and includes a call to the database using sequelize)
const express = require('express')
const proxy = require('express-http-proxy')
const User = require('../db/models/user')
const app = express()
module.exports= {
init : init
}
function init(app) {
User.findOne({where: {id: 1}})
.then(user => app.post('/', proxy(user.URL)))
}
And in my main app.js file, I am then doing this:
// ...
app.post('/', function(req, res, next) {
var dynamic = require('./dynamic')
dynamic.init(app)
})
// ...
But I am getting a 503 response when I post using this approach using http-express-proxy, which was not used in his example.
I got it to work like this. I modified the express-http-proxy module and at the top of the SendProxyRequest() method I included this:
if (req.URL) host = req.URL
And then in my app.js file I added a middleware method to set req.URL after a call to the database:
function getProxyURL (req, res, next) {
User.findOne({where: {id: 1}})
.then(user => {
if(user.URL) req.URL = user.URL
next()
})
}
app.post('/', getProxyURL, proxy('www.default.com'))

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');
});

Resources