Skip to content
Permalink
56aa832405
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
49 lines (45 sloc) 1.58 KB
require("dotenv").config();
// Import Koa and Koa Router
const Koa = require("koa");
const Router = require("koa-router");
const cors = require("@koa/cors");
const etag = require("koa-etag");
const conditional = require("koa-conditional-get");
const logger = require("koa-logger");
const bodyParser = require("koa-bodyparser");
const jwt = require("koa-jwt"); // Import Koa JaWT
const jsonwebtoken = require("jsonwebtoken"); // Import JSON Web Token
const connectDB = require("./util/connectdb");
const weatherRoutes = require("./routes/weather");
const yamljs = require("yamljs");
const { koaSwagger } = require("koa2-swagger-ui");
// Create an instance of a Koa application and a new Router
const spec = yamljs.load("./swagger.yaml");
const app = new Koa();
const router = new Router();
app.use(cors()); // This enables CORS for all routes and origins
app.use(conditional());
app.use(etag());
app.use(logger());
app.use(bodyParser());
// app.use(jwt({ secret: process.env.JWT_SECRET }).unless({ path: [/^\/login/] })); // JWT Middleware
app.use(
koaSwagger({
routePrefix: "/docs", // host at /docs
swaggerOptions: { spec },
})
);
// Define a route for the root path
router.get("/", (ctx) => {
ctx.body = "Welcome to the Weather API!";
});
app.use(weatherRoutes.routes());
// Register the router's middleware in the application
app.use(router.routes()).use(router.allowedMethods());
// Define the port number to listen on
const PORT = 3000;
// Start the server and listen on the specified port
app.listen(PORT, () => {
connectDB();
console.log(`Server running on http://localhost:${PORT}`);
});