Permalink
Cannot retrieve contributors at this time
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?
6003CEM-CourseworkCodio/index.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (45 sloc)
1.58 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | |
}); |