Rohit Rai
Rohit Rai

Reputation: 375

Method Overriding of a Node Module

I am using keystone node module to develop cms based pages in my application.

I initialize keystone just by adding it to my js file as: var keystone = require('keystone');
But the problem which i am facing currently is that, the the route for every keystone based cms feature is localhost:3000/keystone/<featue-name>

I want to remove keystone from the url with another name required for the app. Making changes inside the node-module of keystone does the trick!

But if i do an npm update all my changes goes in vain. Normally, in other languages i used to do it it by METHOD OVERRIDING. I don't know about Method Overriding in a node-module.
Is there any other way of doing it?

Upvotes: 2

Views: 703

Answers (2)

robertklep
robertklep

Reputation: 203286

From what I can see in the source, the admin path prefix is configurable (just not documented):

keystone.init({
  ...
  'admin path' : 'your-own-path',
  ...
});

(which would make the path localhost:3000/your-own-path/<feature-name>)

If you're not using the latest Keystone, perhaps this might work (although it's a bit of a hack):

keystone.pre('routes', function(req, res, next) {
  req.url = req.url.replace(/^\/your-own-path/, '/keystone');
  next();
});

What this does is replace a /your-own-path prefix in requested URL's with /keystone, sort of like an "internal redirect".

Upvotes: 3

Oskar
Oskar

Reputation: 2083

Making changes to the source code in node_modules is a not a good way of solving it since it's not version controlled so everyone working on the repo will have to change it manually. Instead you can fork the repo to your own Github account, do the changes and then install that version of Keystone:

$ npm install --save keystone@git+https://github.com/your-username/keystone.git

Even better, make a fix that defaults to /keystone but is possible to change with the Keystone options and create a PR back to Keystone! The use after you have fixed it should look something like this:

keystone.set('url', 'custom'); // Changes /keystone to /custom

Upvotes: 1

Related Questions