Comments (5)
That is expected because that preHandler is applied to your not found handler as well.
This should solve it:
'use strict'
const Fastify = require('fastify')
const fastifyCors = require('@fastify/cors');
const hookLoggingPlugin = require('./hookLogging');
(async function startServer () {
const fastify = Fastify({
logger: {
transport: {
target: 'pino-pretty',
},
},
});
fastify.register(hookLoggingPlugin);
fastify.register(async function (fastify) {
fastify.register(fastifyCors, {
// hook: 'preHandler',
delegator: (req, callback) => {
const corsOptions = { origin: false };
if (/^success$/m.test(req.headers.origin)) {
corsOptions.origin = true;
}
callback(null, corsOptions);
},
});
fastify.get('/', async function (request, reply) {
return reply.code(200).type('text/plain').send('Hello world');
})
fastify.post('/', async function (request, reply) {
return reply.code(200).type('text/plain').send('Hello world');
})
fastify.setNotFoundHandler(async (_request, reply) => {
reply.code(404).type('text/plain').send('Not found');
});
})
return fastify.listen({ port: 3000 });
})();
from fastify-cors.
That did not resolve it
from fastify-cors.
Interestingly, this does not happen when using fastify-cli
from fastify-cors.
Oh, I've
This should solve it:
```js
'use strict'
const Fastify = require('fastify')
const fastifyCors = require('@fastify/cors');
const hookLoggingPlugin = require('./hookLogging');
(async function startServer () {
const fastify = Fastify({
logger: {
transport: {
target: 'pino-pretty',
},
},
});
fastify.register(async function (fastify) {
// the logging should be inside ;)
fastify.register(hookLoggingPlugin);
fastify.register(fastifyCors, {
// hook: 'preHandler',
delegator: (req, callback) => {
const corsOptions = { origin: false };
if (/^success$/m.test(req.headers.origin)) {
corsOptions.origin = true;
}
callback(null, corsOptions);
},
});
fastify.get('/', async function (request, reply) {
return reply.code(200).type('text/plain').send('Hello world');
})
fastify.post('/', async function (request, reply) {
return reply.code(200).type('text/plain').send('Hello world');
})
fastify.setNotFoundHandler(async (_request, reply) => {
reply.code(404).type('text/plain').send('Not found');
});
})
return fastify.listen({ port: 3000 });
})();
from fastify-cors.
Thank you Matteo!!
from fastify-cors.
Related Issues (20)
- Lack of support with Fastify 4.x HOT 4
- doesnt' work with Typescript HOT 1
- Origin returns undefined HOT 14
- Latency when using a large JWT token HOT 7
- UnhandledPromiseRejectionWarning: AvvioError: Plugin must be a function or a promise HOT 1
- Allow the special `*` value in `array` in `origin` option HOT 3
- Setting for `Cache-Control` to allow preflight requests to be cached in CDNs HOT 1
- Error(s) in doc. HOT 1
- cacheControl default value is missing HOT 1
- Access-Control-Allow-Origin is not working when streaming the data HOT 6
- AvvioError [Error]: Plugin did not start in time: '[object Promise]' HOT 2
- Support Private Network CORS HOT 8
- Options Request Returns 404 running in bun HOT 1
- Missing type declaration file for β@fastify/corsβ module HOT 2
- preflight 404 on prefixed index routes in nested/scopted cors setups
- Multiple @fastify/cors HOT 2
- Do not always define `Vary` header HOT 1
- Vary with dynamic config
- Credentials header is added regardless of origin HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-cors.