feat: init-express-backend #6
@ -1,23 +1,32 @@
|
|||||||
import express from "express";
|
import express from "express"
|
||||||
/**
|
/**
|
||||||
* Initialize Express application instance.
|
* Initialize Express application instance.
|
||||||
* @returns An initialized Express application object.
|
* @returns An initialized Express application object.
|
||||||
*/
|
*/
|
||||||
const app = express();
|
const app = express()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle GET request for homepage route ('/').
|
* Handle GET request for homepage route ('/welcome').
|
||||||
* Send back a simple json response.
|
* Send back a simple json response.
|
||||||
* @param {express.Request} req - HTTP Request object.
|
* @param {express.Request} req - HTTP Request object.
|
||||||
* @param {express.Response} res - HTTP Response object.
|
* @param {express.Response} res - HTTP Response object.
|
||||||
*/
|
*/
|
||||||
function getWelcome(req: express.Request, res: express.Response) {
|
function getWelcome(req: express.Request, res: express.Response) {
|
||||||
const out = {hello:"world"};
|
const out = {hello:"world"}
|
||||||
res.send(out);
|
res.send(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get("/welcome", getWelcome);
|
/**
|
||||||
|
* @openapi
|
||||||
|
* /welcome:
|
||||||
|
* get:
|
||||||
|
* description: Welcome to swagger-jsdoc!
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Returns a welcome json.
|
||||||
|
*/
|
||||||
|
app.get("/welcome", getWelcome)
|
||||||
|
|
||||||
export default app;
|
export default app
|
@ -1,10 +1,16 @@
|
|||||||
import * as dotenv from "dotenv";
|
import * as dotenv from "dotenv";
|
||||||
import app from "./app";
|
import app from "./app";
|
||||||
|
import options from "./swaggerDef";
|
||||||
|
const swaggerJsdoc = require("swagger-jsdoc"),
|
||||||
|
swaggerUi = require("swagger-ui-express");
|
||||||
|
|
||||||
dotenv.config({path: '../../.env'})
|
dotenv.config({path: '../../.env'})
|
||||||
|
|
||||||
const port = parseInt(process.env.BAR_PORT || '3000')
|
const port = parseInt(process.env.BAR_PORT || '3000')
|
||||||
|
|
||||||
|
const specs = swaggerJsdoc(options)
|
||||||
|
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs))
|
||||||
|
|
||||||
app.listen(port, () =>{
|
app.listen(port, () =>{
|
||||||
console.log(`serveur running in ${port}`)
|
console.log(`serveur running in ${port}`)
|
||||||
})
|
})
|
25
Express/barAndCafe/src/swaggerDef.ts
Normal file
25
Express/barAndCafe/src/swaggerDef.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import * as dotenv from "dotenv";
|
||||||
|
dotenv.config({path: '../../.env'})
|
||||||
|
const port = parseInt(process.env.BAR_PORT || '3000')
|
||||||
|
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
definition: {
|
||||||
|
openapi: "3.1.0",
|
||||||
|
info: {
|
||||||
|
title: "LogRocket Express API with Swagger",
|
||||||
|
version: "0.1.0",
|
||||||
|
description:
|
||||||
|
"This is a simple CRUD API application made with Express and documented with Swagger",
|
||||||
|
},
|
||||||
|
servers: [
|
||||||
|
{
|
||||||
|
url: "http://localhost:"+port.toString(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
apis: ['./src/*.ts'],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default options
|
Loading…
x
Reference in New Issue
Block a user