Reputation: 1
I'm working on a Fastify application where I have set up various routes, including one to export data to an Excel file. However, when I try to access the /export-excel route via a POST request, I'm receiving a 404 error with the message: "Route POST:/export-excel not found."
Here's the relevant part of my setup:
Main Fastify server file:
import { fastify } from 'fastify';
import cors from '@fastify/cors';
import fastifyStatic from '@fastify/static';
import path from 'path';
import fastifyJwt from '@fastify/jwt';
import fastifyCookie from '@fastify/cookie';
import * as dotenv from 'dotenv';
dotenv.config();
// Import routes
import { exportExcelRoute } from './routes/create-export-excel';
const app = fastify();
app.register(fastifyJwt, {
secret: process.env.JWT_SECRET || 'default-secret-key',
cookie: {
cookieName: 'refreshToken',
signed: false,
},
sign: {
expiresIn: '7d',
},
});
app.register(fastifyCookie);
app.register(cors);
// Configure fastify-static to serve files from the "public" folder
app.register(fastifyStatic, {
root: path.join(__dirname, '..', 'public', 'images'),
prefix: '/public/',
});
// Register routes
app.register(exportExcelRoute);
// Start server
app.listen({
host: process.env.HOST_IP_ADDRESS ?? '127.0.0.1',
port: 3333,
}).then(() => {
console.log('HTTP Server Running!');
}).catch((error) => console.log('Error during Data Source initialization:', error));
`
Route definition (create-export-excel.ts):
`import { FastifyInstance } from 'fastify';
import path from 'path';
import ExcelJS from 'exceljs';
export async function exportExcelRoute(app: FastifyInstance) {
app.post('/export-excel', async (request, reply) => {
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet('Land Infos');
// Add data to the worksheet...
const filePath = path.join(__dirname, '..', 'public', 'landInfos.xlsx');
await workbook.xlsx.writeFile(filePath);
return reply.sendFile('landInfos.xlsx');
});
}
I receive on Request:
"message": "Route POST:/export-excel not found",
"error": "Not Found",
"statusCode": 404
}```
Upvotes: 0
Views: 47