Fastify - Route GET:/forest not found

Hi,

I’m struggling with getting started on fastify framework. Thanks for the help ! Really appreciated guys if you can help me on this issue :grinning:

Code on main Fastify file

const { createAgent } = require("@forestadmin/agent");
const {
  createSequelizeDataSource,
} = require("@forestadmin/datasource-sequelize");
module.exports = async function (fastify, opts) {
  await fastify.register(import("@fastify/middie"));

  await db.connect();

  await createAgent({
    authSecret: process.env.FOREST_AUTH_SECRET,
    envSecret: process.env.FOREST_ENV_SECRET,
    isProduction: process.env.NODE_ENV === "production",
  })
    // Create your Sequelize datasource
    .addDataSource(createSequelizeDataSource(db.sequelize))
    // Replace "myFastifyApp" by your Fastify application
    .mountOnFastify(fastify)
    .start();

  // This loads all plugins defined in plugins
  // those should be support plugins that are reused
  // through your application
  fastify.register(bearerAuthPlugin, { keys });
  //

  fastify.register(AutoLoad, {
    dir: path.join(__dirname, "plugins"),
    options: Object.assign({}, opts),
  });

  // This loads all plugins defined in routes
  // define your routes in one of these
  fastify.register(AutoLoad, {
    dir: path.join(__dirname, "routes"),
    options: Object.assign({}, opts),
  });
};

Observed behavior

/forest endpoint is not mounted on fastify web server. It keep getting me 404 not found errors on /forest endpoints

Failure Logs

{"level":30,"time":1702983449798,"pid":7344,"hostname":"WIN11","reqId":"req-b","msg":"Route OPTIONS:/forest not found"}
{"level":30,"time":1702983449798,"pid":7344,"hostname":"WIN11","reqId":"req-b","res":{"statusCode":404},"responseTime":0.2769999988377094,"msg":"request completed"}
{"level":30,"time":1702983449799,"pid":7344,"hostname":"WIN11","reqId":"req-c","req":{"method":"GET","url":"/forest","hostname":"localhost:3000","remoteAddress":"::1","remotePort":54327},"msg":"incoming request"}
{"level":30,"time":1702983449799,"pid":7344,"hostname":"WIN11","reqId":"req-c","msg":"Route GET:/forest not found"}
{"level":30,"time":1702983449799,"pid":7344,"hostname":"WIN11","reqId":"req-c","res":{"statusCode":404},"responseTime":0.1569999996572733,"msg":"request completed"}

Package.json

{
  "name": "forest-admin-poc",
  "version": "1.0.0",
  "description": "This project was bootstrapped with Fastify-CLI.",
  "main": "app.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "tap \"test/**/*.test.js\"",
    "start": "fastify start -l info app.js",
    "dev": "fastify start -w -l info -P app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@fastify/auth": "^4.4.0",
    "@fastify/autoload": "^5.0.0",
    "@fastify/bearer-auth": "^9.1.0",
    "@fastify/middie": "^8.3.0",
    "@fastify/sensible": "^5.0.0",
    "@forestadmin/agent": "^1.36.10",
    "@forestadmin/datasource-sequelize": "^1.5.26",
    "dotenv": "^16.3.1",
    "fastify": "^4.0.0",
    "fastify-cli": "^5.9.0",
    "fastify-plugin": "^4.0.0",
    "mariadb": "^3.2.2",
    "sequelize": "^6.35.2"
  },
  "devDependencies": {
    "tap": "^16.1.0"
  }
}

Hi @vice-versa ,

Do you see a success message when you start your agent, like “Successfully mounted on Fastify” ? Or any other related log ?

Can you access other routes of your fastify app ?

If this is your main fastify file, maybe you just forgot to listen for the requests:

await fastify.listen(Number(process.env.HTTP_PORT_FASTIFY));

Thanks a lot for your help Enki !!!

Hello ! Yes I see this successfull mounted message.

Yes I can access all my routes that are dynamically loaded in routes folder

I added this code sample. I have a different error so should be that. I’m currently working on it but it seems strange due to the fact that I’m using await when I register @fastify/middie

AvvioError [Error]: Plugin did not start in time: 'async function (fastify, opts) { -- await fastify.register(import("@fastify/middie"));'. You may have forgotten to call 'done' function or to resolve a Promise

I’m not sure to understand where your fastify instance comes from.
Maybe the .listen is called before the agent.mountOnFastify, because you can access other routes.

Please look at our documentation for fastify setup, you will be able to compare your code with the example:
https://docs.forestadmin.com/developer-guide-agents-nodejs/getting-started/install/expose-an-http-endpoint/using-fastify

This code is coming from fastifly-cli generate project command : fastify/fastify-cli: Run a Fastify application with one command! (github.com)

Indeed, you were right ! I removed the module.exports = async function (f, opts) {} from fastify-cli generated, back to a simple app.js code and it works.

After run with node app.js, it works like a charm :wink:

Thanks for your help !

You’re welcome ! have a nice day !