Skip to content
Snippets Groups Projects
Commit dff70dd2 authored by Yannis Barlas's avatar Yannis Barlas
Browse files

feat(server): cors config to allow client running on a different port

parent c308cf91
No related branches found
No related tags found
No related merge requests found
...@@ -65,6 +65,20 @@ cron.schedule('* * * * * *', () => { ...@@ -65,6 +65,20 @@ cron.schedule('* * * * * *', () => {
The library that enables this is `node-cron`. Be sure to check its [documentation](https://github.com/node-cron/node-cron#node-cron) for further details. The library that enables this is `node-cron`. Be sure to check its [documentation](https://github.com/node-cron/node-cron#node-cron) for further details.
### CORS support for the client
If you run your client on a different host/port than the server, you might run into issues where cross-origin requests are rejected. If that happens, make sure the following entries exist in your config. The server should take care of it once these are defined.
```js
// replace values with the ones you are using
{
'pubsweet-client': {
host: 'http://localhost',
port: 4000,
}
}
```
### Other exports from included packages ### Other exports from included packages
- `createJWT` is an export of a function in `pubsweet-server` that does just that. Useful if you have custom login resolvers. - `createJWT` is an export of a function in `pubsweet-server` that does just that. Useful if you have custom login resolvers.
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"config": "^3.3.1", "config": "^3.3.1",
"cookie-parser": "^1.4.5", "cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"helmet": "^3.22.0", "helmet": "^3.22.0",
"http-status-codes": "^1.4.0", "http-status-codes": "^1.4.0",
......
...@@ -4,6 +4,7 @@ const path = require('path') ...@@ -4,6 +4,7 @@ const path = require('path')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const config = require('config') const config = require('config')
const cors = require('cors')
const cookieParser = require('cookie-parser') const cookieParser = require('cookie-parser')
const express = require('express') const express = require('express')
const helmet = require('helmet') const helmet = require('helmet')
...@@ -55,6 +56,16 @@ const configureApp = app => { ...@@ -55,6 +56,16 @@ const configureApp = app => {
) )
} }
// Allow CORS from client if host / port is different
app.use(
cors({
origin: `${config.get('pubsweet-client.host')}:${config.get(
'pubsweet-client.port',
)}`,
credentials: true,
}),
)
// Register passport authentication strategies // Register passport authentication strategies
app.use(passport.initialize()) app.use(passport.initialize())
const authentication = require('pubsweet-server/src/authentication') const authentication = require('pubsweet-server/src/authentication')
......
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment