I'm trying to find a way to add routes dynamically outsides of the config/routes.js file.
I'm creating a module who need some routes and don't want to let the user add it manually under config/routes.js.
Is there a way to do this ? I look sails doc and maybe with hooks I can't achieve this but can't find how
Thanks
I'm curious What kind of routing path you want to create?
Remember that wildcard is available in route.js
'GET /validEmail/*', "VerificationController.verifyEmail"'
Related
I'm looking to create a single node.js application that will render multiple different client websites.
We currently run one node.js application per client website, but I think this might be overkill as the serverside logic is exactly the same for all of them, the only difference being the handlebars template.
I'm looking to re-architecture to have a single node.js application which will then render the different client's websites based on some incoming information, will use nginx to add a header or something to the request so the app knows which website to render.
Is it possible to store the handlebars template within a database and then request the template at render time? Rendering a simple single page should be easy enough, but I'm struggling to understand how partials would be rendered?
Looking to achieve something similar to Shopify's Storefront Renderer, not sure if it's possible to do with handlebars or if it's better to use of of the LiquidJs ports for Node to achieve this?
https://shopify.engineering/how-shopify-reduced-storefront-response-times-rewrite
I do this with different countries, same concept as you with different Clients.
You just create the Handlebars layout and put it in your partials folder. No need to put template in your database.
Then your server logic will have eg clientName, and your Handlebars will have IfEqual tags (you'll need an IfEqual helper)
Example:
Server route will give a variable clientName.
Handlebars you have the main view page with only the IfEqual helpers.
{{#ifEqual clientName 'Client Name 1'}}
{{> client/clientName1}}
{{/ifEqual}}
{{#ifEqual clientName 'Client Name 2'}}
{{> client/clientName2}}
{{/ifEqual}}
Your helper function will be
, helpers: {
ifEqual: function(x, y, options) {
return(x == y) ? options.fn(this) : options.inverse(this)
} // {{#ifEqual statusLogin 'unconfirmed'}} {{/ifEqual}}
Search nodejs handlebar helper if you dont know how to set it.
So my partial folder, you can create a folder in it called client, and put all your Handlebars template in there. Search for nodejs handlebars partial folder setup if you dont know how to do it.
The handlebar helper function I found it I think in stack overflow, so you can search for others if you want, but for me it works.
Your partial template files just do as normal Handlebar files.
So the main idea is User loads the page, depending on what Client flag you put it, it loads up that template.
I was wondering if anyone has a way to automatically and programmatically load HapiJS routes automatically. I was looking for a way that would be something like the routes that fall under a specific resource all go in a js file named after that resource.
For example, if I had a file src/routes/account.js, which would have the routes /login and /register, which would create the API routes /account/login and /login/register.. Or something that would let me have a programmatic way of automatically loading the routes.
I use Actin to load my controllers, and I was hoping to use something similar to that. I didn't see any plugins that could accomplish this, so I thought id ask if someone has a method for this already
Thanks!
I couldn't find anything that would load routes programmatically, using a folder structure to help with the route hierarchy, so I created my own.
It's not a full HapiJS plugin yet, but heres the code if anyone wants to use it.
Basic Details
Load the routes.js file as a HapiJS plugin (from the /dist folder, for ES5 transpiled version)
You can load it any way you want, I use Confidence to load it in the configuration file
Create a *Routes folder to contain your routes, make sure it's in the same folder as the routes.js file (Ill make an option so you can specify the routes folder later)
Create some js files that export some HapiJS routes (like so).
Keep in mind that the path in the route files will be appended to the path from the routes folder. Meaning if you have a file at src/routes/users.js, and it has a route with the path /list, then the real path will be /users/list
To define a Root Resource, then define the rootResource in the settings (Value should be the file name without the .js extension)
Take a look at hapi-auto-route. This package loads routes automatically and add prefix to the route path
I am using Sails Framework with the newrelic plugin. I am currently trying to register the app based on a flag in production.js/development.js. This is the flag in development.js:
ENABLE_NEWRELIC_NODE_SERVICE: false,
And this is the piece of code in App.js:
if (sails.config.ENABLE_NEWRELIC_NODE_SERVICE){
require('sails-hook-newrelic/register');
}
But it seems that sails.config is not accessible in app.js (I might be wrong). Is there any other way to include conditionality in App.js based on config files?
Thanks!
Short of a Sails specific way of doing this, simply require in the config file:
var config = require('./sails.config') // Check the path is correct
Then use the flag:
if (config.ENABLE_NEWRELIC_NODE_SERVICE){
require('sails-hook-newrelic/register');
}
The sails.config object isn't available until Sails is actually loaded. Looking at the code of sails-hook-newrelic, it's not clear to me why register() needs to be called so early; it seems like it could just happen as part of the hook initialization. But, the code isn't written that way, so you're stuck on that point. Looks like there's a pull request open to fix it. In the meantime, assuming that you want to activate New Relic based on the Node environment, you can just check process.NODE_ENV in your app.js:
if (process.NODE_ENV == 'development') {
require('sails-hook-newrelic/register');
}
Just make sure that you start your app with NODE_ENV=development node app.js.
I want to create href links inside my views, but I do not want to point to a static address. I want to dynamically create href urls based on routes.
Example:
// app.js:
app.get('/test', function testPage(req, res, next) {
// do something here
});
// inside view.html:
<a href="path('testPage')" /> // something similar
<a href="/test" /> I do NOT want this!
Am I missing something, or there is no way to do that easily?
I've searched and I found that app._router.stack has all routes. But they are just a plain ARRAY so they don't have any kind of id or smth! What I came up with, is a function that parses all routes (from stack), gets their name, which is set only if the function has a name (http://expressjs.com/4x/api.html#req.route) and then create an object with key (route name) -> path.
Then I can use this function (somehow) inside views to create paths, right?
Any better ideas?
Coming from PHP frameworks background, I am used to two way routes. However, from what I've seen in tutorials, people just use the static links. I think it has to do with the nature of Node.js web applications. Lots of them are SPAs, so you just build an API and then refer to it statically from the frontend application.
When researching this topic, I've came accross this little library called 2-way router which can be used with Express. I haven't experimented with it yet, but it might be useful to you.
EDIT: Tried to re-word my search and I've found similar thread about url generation and express route naming.
EDIT: You can also implement django-like routing.
I am using express.js for my website. I have created the directory structure using 'express-generator'. It contains 'views' folder to render views. It also has 'routes' folder for routing purpose. Now my question is where should I define or do the core logic code?
For organization purposes, I use /controllers for my, well, controllers (which I believe is what you mean). Express really doesn't care where you put your code, just as long as you correctly use require('./path/to/whatever/directoryname/you/choose/filename') in your routes and views and module.exports = controllerToExpose; in your controllers.