Closed karmabadger closed 2 years ago
do you have "routes" directory in "src" ?
can you also provide code how you used fastify-autoroutes
?
do you have "routes" directory in "src" ?
can you also provide code how you used
fastify-autoroutes
? index.ts:import fastify, { FastifyInstance } from "fastify"; import * as autoroutes from "fastify-autoroutes";
const app: FastifyInstance = fastify({ logger: true });
const startServer = async () => { await app.register(autoroutes.default, { dir: "./src/routes", prefix: "/api/v1", // -> optional });
await app.listen(3000); };
startServer();
src/routes/some.ts:
```ts
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { Resource } from 'fastify-autoroutes'
export default (fastify: FastifyInstance) => <Resource> {
get: {
handler: async (request: FastifyRequest, reply: FastifyReply) => 'Hello, Route!'
}
}
if you cwd is on src, than, 'dir' should be only 'routes'
if you cwd is on src, than, 'dir' should be only 'routes' no I'm running index.ts from the root dir whcih is not in src. is it better to have it all in src instead?
your index.ts should be in "src" folder (usually this is the way is done) than you have to run the program using "src" (or dist, if it's builded) as cwd
example: ... "scripts": { "start": "node dist", "dev": "ts-node-dev src" }
in this case, if you use "routes", the plugin will know he have to take from "<your folder location>/src" (or dist if is builded)
your index.ts should be in "src" folder (usually this is the way is done) than you have to run the program using "src" (or dist, if it's builded) as cwd
example: ... "scripts": { "start": "node dist", "dev": "ts-node-dev src" }
in this case, if you use "routes", the plugin will know he have to take from "<your folder location>/src" (or dist if is builded)
whether it is in src shouldn't really matter right? it's just mostly depending on how the devs are used to?
I'm going to try it anyways
ts-node src/index.ts
C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4
return new (P || (P = Promise))(function (resolve, reject) {
^
Error: fastify-autoroutes dir C:\Users\wenxu\Projects\test\fileroutes\src\index.ts\routes does not exists
at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:116:19
at Generator.next (<anonymous>)
at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:8:71
at new Promise (<anonymous>)
at __awaiter (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4:12)
at exports.default.fastify_plugin_1.default.fastify (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:98:7)
at Plugin.exec (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:132:19)
at Boot.loadPlugin (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:274:10)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
ts-node src
if you start with ts-node src/index.ts
the cwd is not setted to 'src', so the routes should be "../routes".
Usually, if you are not running a script, you run a module by the index file (so just "node dist (or ts-node src in your case)"
I opened a pull request on your repo, hope this help :)
Versions
Behavior
from this repo: https://github.com/karmabadger/fastify-routes-test
when running:
npm run dev
when running:
C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:44 return next(new Error(
${exports.ERROR_LABEL} dir ${dirPath} does not exists
)); ^Error: fastify-autoroutes dir C:\Users\wenxu\Projects\test\fileroutes\build\index.js\src\routes does not exists at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:44:21 at Generator.next ()
at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:8:71
at new Promise ()
at __awaiter (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4:12)
at exports.default.fastify_plugin_1.default.fastify (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:31:72)
at Plugin.exec (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:132:19)
at Boot.loadPlugin (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:274:10)
at processTicksAndRejections (node:internal/process/task_queues:83:21)