オンラインショッピングモールの例-フォルダ、ファイル構造(1)


フォルダ構造とファイルの理解


SERVERフォルダ


IN-SPRINT-CMARKET-DATABASE > server
.env, .gitignore, app.js, package.json, package-lock.json, routes.js, schema.sql, seeds.sql
config > config.js
controllers > index.js
db > index.js
models > index.js

.env

DATABSE_SPRINT_PASSWORD='내 비밀번호'

.gitignore

node_modules
.env

app.js

const express = require('express');
const router = require('./routes');
const cors = require('cors');
const morgan = require('morgan');
const parser = require('body-parser');
const controller = require('./controllers');

const app = express();
const port = 4000;

app.use(
  morgan(':method :url :status :res[content-length] - :response-time ms')
);

app.use(cors());
app.use(parser.json());
app.use("/users",router);
app.get("/items",controller.items.get);
module.exports = app.listen(port, ()=>{
  console.log('server is starting on ${port}');
});

routes.js

const router = require('express').Router();
const controller = require('./controllers');

router.get('/:userId/orders', controller.orders.get);
router.post('/:userId/orders/new', controller.orders.post);

module.exports = router;

schema.sql


テーブル生成コマンドmysql -u root <server/schema.sql -p -Dcmarket=>
CREATE TABLE users (
  id INT AUTO_INCREMENT,
  username varchar(255),
  PRIMARY KEY(id)
);

CREATE TABLE items(
  id INT AUTO_INCREMENT,
  name varchar(255),
  price INT,
  image varchar(255),
  PRIMARY KEY(id)
);

CREATE TABLE orders(
  id INT AUTO_INCREMENT,
  user_id INT,
  total_price INT,
  created_at datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(id)
);

CREATE TABLE order_items(
  id INT AUTO_INCREMENT,
  order_id INT,
  item_id INT,
  order_quantity INT,
  PRIMARY KEY(id)
);

ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id);
ALTER TABLE order_items ADD FOREIGN KEY (order_id) REFERENCES orders (id);
ALTER TABLE order_items ADD FOREIGN KEY (item_id) REFERENCES items (id);

seed.sql


テーブルにデータを挿入するコマンド:mysql -u root <server/seed.sql -p -Dcmarket=>
INSERT INTO items (name, price, image) VALUES ("노른자 분리기", 9900, "../images/egg.png"), ("2020년 달력", 12000, "../images/2020.jpg"), ("개구리 안대", 2900, "../images/frog.jpg"), ("뜯어온 보도블럭", 4900, "../images/block.jpg"), ("칼라 립스틱", 2900, "../images/lip.jpg"), ("잉어 슈즈", 3900, "../images/fish.jpg"), ("웰컴 매트", 6900, "../images/welcome.jpg"), ("강시 모자", 9900, "../images/hat.jpg");
INSERT INTO users (username) VALUES ("김코딩")