0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

シフト管理CRUDアプリ ~サーバーとRouter~

0
Last updated at Posted at 2026-01-21

server..js

アプリ全体の入口

目的

「HTTPリクエストをどの処理に流すかを決める」

server.js
import express from 'express';
import { create, getId, update, getAll, deleteId } from './controllers/shift.controller';

const app = express();
const PORT = 8080;

// JSONを読むミドルウェアの作成
app.use(express.json());

// 新規作成
app.post("/api/shifts", function(req, res){
  create(req, res);
  res.json(create);
});

// 全件取得
app.get("/api/shifts", function(req, res){
  getAll(req, res);
});

// id検索
app.get("/api/shifts/:id", function(req, res){
  getId(req, res);
});

// シフトの更新
app.put("/api/shifts/:id", function(req, res){
  update(req, res);
});

// シフトの削除
app.delete("/api/shifts/:id", function(req, res){
  deleteId(req, res);
});

app.listen(PORT, function(){
  console.log('サーバーの起動に成功しました');
});

応用
ラップ関数を減らす

shift.routes.js

Expressのルーティング設定

役割

・URLとHTTPメソッドを定義する
・どのコントローラーを使うのか決める

shift.routes.js
import * as contoroller from '../controllers/shift.controller';
import express from 'express';

export default (app) => {
  let router = express.Router();

  router.post("/", contoroller.create);
  router.get("/", contoroller.getAll);
  router.get("/:id", contoroller.getId);
  router.put("/:id", contoroller.update);
  router.delete("/:id", contoroller.deleteId);

  app.use("/api/shifts", router);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?