Skip to content

Instantly share code, notes, and snippets.

@katychuang
Last active October 16, 2024 19:05
Show Gist options
  • Save katychuang/f94b394551e03ec18c8b4e47d3bf4042 to your computer and use it in GitHub Desktop.
Save katychuang/f94b394551e03ec18c8b4e47d3bf4042 to your computer and use it in GitHub Desktop.

Revisions

  1. katychuang revised this gist Oct 16, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion api.js
    Original file line number Diff line number Diff line change
    @@ -25,11 +25,12 @@ app.get("/", (req, res, next) => {
    res.send("API end points include /one, /two");
    });

    // List all records in top100 table
    // the `/one` endpoint showing hard coded json object
    app.get("/one", (req, res, next) => {
    res.status(200).json({"name": "one"});
    });

    // the `/two` endpoint
    app.post("/two", (req, res) => {
    res.status(200).json(req.body);
    });
  2. katychuang revised this gist Oct 16, 2024. No changes.
  3. katychuang revised this gist Oct 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion makefile
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@ default:
    node api.js

    install:
    npm install expressjs
    npm install express
  4. katychuang revised this gist Oct 15, 2024. 2 changed files with 1 addition and 6 deletions.
    2 changes: 1 addition & 1 deletion makefile
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@ default:
    node api.js

    install:
    npm install expressjs
    npm install expressjs
    5 changes: 0 additions & 5 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    {
    "dependencies": {
    "express": "^4.19.2"
    }
    }
  5. katychuang created this gist Oct 15, 2024.
    40 changes: 40 additions & 0 deletions api.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const express = require("express");
    const app = express();
    const router = express.Router();
    app.use(express.json());

    const port = 3000;

    // Start server
    app.listen(port, () => {
    console.log("Server running on port %PORT%".replace("%PORT%",port))
    });

    // Insert here other API endpoints
    /*
    GET Requests
    /
    /one
    POST Requests
    /two
    */
    // Root endpoint
    app.get("/", (req, res, next) => {
    res.send("API end points include /one, /two");
    });

    // List all records in top100 table
    app.get("/one", (req, res, next) => {
    res.status(200).json({"name": "one"});
    });

    app.post("/two", (req, res) => {
    res.status(200).json(req.body);
    });

    // Default response for any other request
    app.use(function(req, res){
    res.status(404);
    });
    5 changes: 5 additions & 0 deletions makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    default:
    node api.js

    install:
    npm install expressjs
    5 changes: 5 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    {
    "dependencies": {
    "express": "^4.19.2"
    }
    }