KeystoneJS: Cors issue - keystonejs

i want to enable the CORS. so I set the keystone.set('cors allow origin', true);
and have this in my router:
app.all('/api*', keystone.middleware.cors);
but it doesn't work.
I have to change createDynamicRouter in keystone/lib and add this in
router.all('/api*', function (req, res, next) {
console.log('------------------------------------------------');
console.log('Notice: Enabling CORS headers.');
console.log('------------------------------------------------');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow Methods','GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
i created a model and using the auto-created routers.
did I do something wrong? I want to enable cors in my code, not in the keystone core code.
thanks.
mikeso

This GitHub Issue may be of use to you. Namely, you may need to add this code to your routes/index.js file:
app.all('/api*', keystone.middleware.cors);
app.options('/api*', function(req, res) {
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-XSRF-TOKEN');
res.sendStatus(200);
});

Related

Nuxt serverMiddleware always return 200 code

I'm trying to handle request errors on the ssr side, for this I use serverMiddleware, but the problem is that res.statusCode is always 200. Although in the browser I see the page crashed with 500 code. Feeling as if the middleware fires before my api requests. Is there any way to solve this problem?
server-middleware/logger.js
export default function (req, res, next) {
console.log('Server middleware', res.statusCode);
next()
}
nuxt.config.js
serverMiddleware: ['~/server-middleware/logger'],

Needed explanation on Express MiddleWares with next

I have only one MiddleWare with next called inside, and the request-response cycle is ended even though i did not use the res.send(), how is that?
app.use("/", function (req, res, next) {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
});

Express route with optional parameter?

I have an Express route for public-sitemap.xml:
app.get("/public-sitemap.xml", function(req, res) {
// do stuff
});
I'd like to support URLs like public-sitemap-1.xml, public-sitemap-2.xml as well.
How can I allow these optional parameters in Express?
If I set the route to /public-sitemap-?:id?.xml that allows public-sitemap-1.xml etc, but it also allows public-sitemap1.xml, which I'd prefer to reject.
Is this what you're looking for?
/public-sitemap(-:id?)?.xml
More info here: https://expressjs.com/en/guide/routing.html
Use this:
app.get("/public-sitemap-:id(\\d+).xml", function(req, res) {
// do stuff
});

Express handle routes with regex

I have an route like as '/antalya-ucak-bileti' and 'antalya' word will be dynamically change. How can I handle it in routes?
I'm used NodeJS and ExpressJS
Thanks
This should work:
app.get('/:destination-ucak-bileti', (req, res) => {
console.log('P', req.params.destination);
res.end();
});

Express.js and intercepting .js requests and serving .gz instead

So, I am using webpack to compress/gzip my js files. Then I want to use my Express server to serve those up when a .js request comes in. I am having a devil of a time getting this to work in production. I am able to in dev. I feel it has to do with how I am setting my static files. Assitance?
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
// THIS is not working
app.get("*.js", function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
next();
});
I tried positioning above, middle etc.. of the above code. Not working. I know my webpack is building it as I see the output in the build folder. I just can't seem to get my express server to serve up the .gz version.
My guess is that in production you are running version 2.x and in dev you are using 3.x.
As per http://51elliot.blogspot.com/2012/08/serve-gzipped-files-with-expressjs.html you can see that...
For 2.x
// basic URL rewrite to serve gzipped versions of *.min.js files
app.get('*', function (req, res, next) {
req.url = req.url + '.gz';
res.header('Content-Encoding', 'gzip');
next();
});
and for 3.x:
app.get('*', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
next();
});
NOTE: I have not tried this, just a hunch.