Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
// IMPORT PACKAGES
// Here you should import the required packages for your Express app: `express` and `morgan`

import express from "express";
import morgan from "morgan";
import path from "path";
import { fileURLToPath } from "url";

import projects from "./data/projects.json" with { type: "json" };
import articles from "./data/articles.json" with { type: "json" };

// CREATE EXPRESS APP
// Here you should create your Express app:

const app = express();

// __dirname (ES Modules)
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// MIDDLEWARE
// Here you should set up the required middleware:
// - `express.static()` to serve static files from the `public` folder
// - `express.json()` to parse incoming requests with JSON payloads
// - `morgan` logger to log all incoming requests


app.use(express.static("public"));
app.use(express.json());
app.use(morgan("dev"));

// =======================
// ROUTES
// Start defining your routes here:



// START THE SERVER
// Make your Express server listen on port 5005:
// =======================

// Home
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "views", "home.html"));
});

// Blog
app.get("/blog", (req, res) => {
res.sendFile(path.join(__dirname, "views", "blog.html"));
});

// Projects API
app.get("/api/projects", (req, res) => {
res.json(projects);
});

// Articles API
app.get("/api/articles", (req, res) => {
res.json(articles);
});

// 404
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, "views", "not-found.html"));
});

// START SERVER
app.listen(5005, () => {
console.log("Server listening on port 5005");
});
Loading