Created server and client
This commit is contained in:
@@ -2,35 +2,63 @@ import Fastify from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import jwt from '@fastify/jwt';
|
||||
import multipart from '@fastify/multipart';
|
||||
import static from '@fastify/static';
|
||||
import fastifyStatic from '@fastify/static';
|
||||
import { join } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname } from 'path';
|
||||
import dotenv from 'dotenv';
|
||||
import authRoutes from './routes/auth.js';
|
||||
|
||||
const app = Fastify({ logger: true });
|
||||
// Load environment variables
|
||||
dotenv.config();
|
||||
|
||||
app.register(cors, {
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
const app = Fastify({
|
||||
logger: true,
|
||||
ajv: {
|
||||
customOptions: {
|
||||
removeAdditional: false,
|
||||
useDefaults: true,
|
||||
coerceTypes: true,
|
||||
allErrors: true
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Register plugins
|
||||
await app.register(cors, {
|
||||
origin: process.env.FRONTEND_URL || 'http://localhost:3000'
|
||||
});
|
||||
|
||||
app.register(jwt, {
|
||||
secret: process.env.JWT_SECRET
|
||||
await app.register(jwt, {
|
||||
secret: process.env.JWT_SECRET || 'your-super-secret-key-change-this-in-production'
|
||||
});
|
||||
|
||||
app.register(multipart);
|
||||
await app.register(multipart);
|
||||
|
||||
app.register(static, {
|
||||
root: join(fileURLToPath(import.meta.url), '../../uploads'),
|
||||
await app.register(fastifyStatic, {
|
||||
root: join(__dirname, '../uploads'),
|
||||
prefix: '/uploads/'
|
||||
});
|
||||
|
||||
app.register(import('./routes/auth.js'));
|
||||
app.register(import('./routes/posts.js'));
|
||||
app.register(import('./routes/tags.js'));
|
||||
app.register(import('./routes/images.js'));
|
||||
// Register routes
|
||||
await app.register(authRoutes);
|
||||
|
||||
// Testing route
|
||||
app.get('/', async (request, reply) => {
|
||||
return { hello: 'world' }
|
||||
});
|
||||
|
||||
// Start the server
|
||||
const start = async () => {
|
||||
try {
|
||||
await app.listen({ port: 3001 });
|
||||
await app.listen({
|
||||
port: process.env.PORT || 3001,
|
||||
host: process.env.HOST || 'localhost'
|
||||
});
|
||||
console.log(`Server listening on ${app.server.address().port}`);
|
||||
} catch (err) {
|
||||
app.log.error(err);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user