Showing
11 changed files
with
174 additions
and
2 deletions
Project/.babelrc
0 → 100644
Project/app.js
0 → 100644
| 1 | +import "@babel/polyfill"; | ||
| 2 | +import dotenv from "dotenv"; | ||
| 3 | +import express from "express"; | ||
| 4 | +import morgan from "morgan"; | ||
| 5 | +import helmet from "helmet"; | ||
| 6 | +import cookieParser from "cookie-parser"; | ||
| 7 | +import bodyParser from "body-parser"; | ||
| 8 | +import mongoose from "mongoose"; | ||
| 9 | +import session from "express-session"; | ||
| 10 | +import flash from "express-flash"; | ||
| 11 | +import MongoStore from "connect-mongo"; | ||
| 12 | +import { localsMiddleware } from "./middlewares"; | ||
| 13 | +import routes from "./routes"; | ||
| 14 | + | ||
| 15 | +dotenv.config(); | ||
| 16 | +const app = express(); | ||
| 17 | + | ||
| 18 | +const CokieStore = MongoStore(session); | ||
| 19 | + | ||
| 20 | +app.use(helmet()); | ||
| 21 | +app.set("view engine", "pug"); | ||
| 22 | +app.use("/uploads", express.static("uploads")); | ||
| 23 | +app.use("/static", express.static("static")); | ||
| 24 | +app.use(cookieParser()); | ||
| 25 | +app.use(bodyParser.json()); | ||
| 26 | +app.use(bodyParser.urlencoded({ extended: true })); // json, html, text, urlencoded 할 거 없이 다 parser할 수 있도록 설정해줘야 한다. | ||
| 27 | +app.use(morgan("dev")); | ||
| 28 | + | ||
| 29 | +app.use( | ||
| 30 | + session({ | ||
| 31 | + secret: process.env.COOKIE_SECRET, | ||
| 32 | + resave: true, | ||
| 33 | + saveUninitialized: false, | ||
| 34 | + store: new CokieStore({ mongooseConnection: mongoose.connection }), | ||
| 35 | + }) | ||
| 36 | +); | ||
| 37 | +app.use(flash()); | ||
| 38 | +//app.use(passport.initialize()); | ||
| 39 | +//app.use(passport.session()); | ||
| 40 | + | ||
| 41 | +app.use(localsMiddleware); | ||
| 42 | + | ||
| 43 | +export default app; // 파일을 불러올때 app object를 준다는 의미. |
Project/assets/js/main.js
0 → 100644
File mode changed
Project/db.js
0 → 100644
| 1 | +/* eslint-disable no-console */ | ||
| 2 | +import mongoose from "mongoose"; | ||
| 3 | +import dotenv from "dotenv"; | ||
| 4 | + | ||
| 5 | +dotenv.config(); | ||
| 6 | + | ||
| 7 | +mongoose.connect(process.env.MONGO_URL, { | ||
| 8 | + useNewUrlParser: true, | ||
| 9 | + useFindAndModify: false, | ||
| 10 | +}); | ||
| 11 | + | ||
| 12 | +const db = mongoose.connection; | ||
| 13 | + | ||
| 14 | +const handleOpen = () => { | ||
| 15 | + console.log("✅ Connected to DB"); | ||
| 16 | +}; | ||
| 17 | +const handleError = (error) => | ||
| 18 | + console.log(`🔴 Error on DB Connection: ${error}`); | ||
| 19 | + | ||
| 20 | +db.once("open", handleOpen); // connection을 열고 성공여부를 확인할 수 있는 function을 만들 것이다. | ||
| 21 | +db.on("error", handleError); |
Project/index.js
deleted
100644 → 0
| 1 | -console.log("hi"); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
Project/init.js
0 → 100644
| 1 | +import dotenv from "dotenv"; | ||
| 2 | +import app from "./app"; // app.js에서 export default app했기 때문에 불러올 수 있다. | ||
| 3 | +import "./db"; | ||
| 4 | + | ||
| 5 | +dotenv.config(); | ||
| 6 | +const PORT = process.env.PORT || 80; | ||
| 7 | + | ||
| 8 | +const handleListening = () => { | ||
| 9 | + console.log(`✅ Listening on: http://localhost:${PORT}`); | ||
| 10 | + // call-back함수. | ||
| 11 | + // PORT를 listen하기 시작할 때 함수를 호출해준다. | ||
| 12 | +}; | ||
| 13 | + | ||
| 14 | +app.listen(PORT, handleListening); |
Project/middlewares.js
0 → 100644
| 1 | +import dotenv from "dotenv"; | ||
| 2 | +import multer from "multer"; | ||
| 3 | +import multerS3 from "multer-s3"; | ||
| 4 | +import aws from "aws-sdk"; | ||
| 5 | +import routes from "./routes"; | ||
| 6 | + | ||
| 7 | +dotenv.config(); | ||
| 8 | + | ||
| 9 | +const s3 = new aws.S3({ | ||
| 10 | + accessKeyId: process.env.AWS_KEY, | ||
| 11 | + secretAccessKey: process.env.AWS_PRIVATEE_KEY, | ||
| 12 | + region: "ap-northeast-2", | ||
| 13 | +}); | ||
| 14 | + | ||
| 15 | +export const localsMiddleware = (req, res, next) => { | ||
| 16 | + res.locals.siteName = "my Storage"; | ||
| 17 | + res.locals.routes = routes; | ||
| 18 | + res.locals.loggedUser = req.user || null; | ||
| 19 | + // console.log(req); | ||
| 20 | + next(); | ||
| 21 | +}; | ||
| 22 | + | ||
| 23 | +export const onlyPublic = (req, res, next) => { | ||
| 24 | + if (req.user) { | ||
| 25 | + res.redirect(routes.home); | ||
| 26 | + } else { | ||
| 27 | + next(); | ||
| 28 | + } | ||
| 29 | +}; | ||
| 30 | +export const onlyPrivate = (req, res, next) => { | ||
| 31 | + if (req.user) { | ||
| 32 | + next(); | ||
| 33 | + } else { | ||
| 34 | + res.redirect(routes.home); | ||
| 35 | + } | ||
| 36 | +}; |
| ... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
| 16 | "fetchSpec": "^8.2.0" | 16 | "fetchSpec": "^8.2.0" |
| 17 | }, | 17 | }, |
| 18 | "_requiredBy": [ | 18 | "_requiredBy": [ |
| 19 | + "#USER", | ||
| 19 | "/" | 20 | "/" |
| 20 | ], | 21 | ], |
| 21 | "_resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", | 22 | "_resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", | ... | ... |
Project/routes.js
0 → 100644
File mode changed
Project/webpack.config.js
0 → 100644
| 1 | +const path = require("path"); | ||
| 2 | +const autoprefixer = require("autoprefixer"); | ||
| 3 | +const ExtractCSS = require("extract-text-webpack-plugin"); | ||
| 4 | + | ||
| 5 | +const MODE = process.env.WEBPACK_ENV; | ||
| 6 | +const ENTRY_FILE = path.resolve(__dirname, "assets", "js", "main.js"); | ||
| 7 | +const OUTPUT_DIR = path.join(__dirname, "static"); | ||
| 8 | + | ||
| 9 | +const config = { | ||
| 10 | + entry: ["@babel/polyfill", ENTRY_FILE], | ||
| 11 | + mode: MODE, | ||
| 12 | + module: { | ||
| 13 | + rules: [ | ||
| 14 | + { | ||
| 15 | + test: /\.{js}$/, | ||
| 16 | + use: [ | ||
| 17 | + { | ||
| 18 | + loader: "babel-loader", | ||
| 19 | + }, | ||
| 20 | + ], | ||
| 21 | + }, | ||
| 22 | + { | ||
| 23 | + test: /\.(scss)$/, | ||
| 24 | + use: ExtractCSS.extract([ | ||
| 25 | + { | ||
| 26 | + loader: "css-loader", | ||
| 27 | + }, | ||
| 28 | + { | ||
| 29 | + loader: "postcss-loader", | ||
| 30 | + options: { | ||
| 31 | + plugins() { | ||
| 32 | + return [autoprefixer({ browsers: "cover 99.5%" })]; | ||
| 33 | + }, | ||
| 34 | + }, | ||
| 35 | + }, | ||
| 36 | + { | ||
| 37 | + loader: "sass-loader", | ||
| 38 | + }, | ||
| 39 | + ]), | ||
| 40 | + }, | ||
| 41 | + ], | ||
| 42 | + }, | ||
| 43 | + output: { | ||
| 44 | + path: OUTPUT_DIR, | ||
| 45 | + filename: "[name].js", | ||
| 46 | + }, | ||
| 47 | + plugins: [new ExtractCSS("style.css")], | ||
| 48 | +}; | ||
| 49 | + | ||
| 50 | +module.exports = config; |
-
Please register or login to post a comment