Skip to content

Commit

Permalink
feat(node): update app generator for fastify to be more inline with f…
Browse files Browse the repository at this point in the history
…astify cli (#14667)
  • Loading branch information
jaysoo committed Jan 27, 2023
1 parent 0f15c14 commit a0ed060
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';

/* eslint-disable-next-line */
export interface AppOptions {
}

export async function app(fastify: FastifyInstance, opts: AppOptions) {
fastify.get('/', async function(request: FastifyRequest, reply: FastifyReply) {
return { message: 'Hello API' };
});
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import fastify from 'fastify';
import Fastify from 'fastify';
import { app } from './app/app';

const host = process.env.HOST ?? 'localhost';
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;

const app = fastify();

app.get('/', async (req, res) => {
return { 'message': 'Hello API'};
// Instantiate Fastify with some config
const server = Fastify({
logger: true,
});

const start = async() => {
try {
await app.listen({ host, port });
console.log(`[ ready ] http://${host}:${port}`);
} catch (err) {
// Errors are logged here
process.exit(1);
}
}
// Register your application as a normal plugin.
server.register(app);

start();
// Start listening.
server.listen({ port, host }, (err) => {
if (err) {
server.log.error(err);
process.exit(1);
} else {
console.log(`[ ready ] http://${host}:${port}`);
}
});

0 comments on commit a0ed060

Please sign in to comment.