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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
.env
__http__
/generated/prisma
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"singleQuote": true,
"trailingComma": "all",
"semi": true,
"printWidth": 100,
"endOfLine": "auto",
"arrowParens": "always"
}
20 changes: 20 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import express from 'express';
import cors from 'cors';
import { errorHandler } from './middlewares/errorHandler.js';
import productRouter from './routes/products.js';
import articleRouter from './routes/articles.js';
import commentRouter from './routes/comments.js';

const app = express();
app.use(cors());
app.use(express.json());
app.use('/uploads', express.static('uploads')); // 이미지 접근
app.use('/products', productRouter);
app.use('/articles', articleRouter);
app.use('/comments', commentRouter);

app.use(errorHandler);

app.listen(process.env.PORT || 3000, () => {
console.log('Server running...');
});
5 changes: 5 additions & 0 deletions middlewares/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const errorHandler = (err, req, res, next) => {
console.error(err);
if (res.headersSent) return next(err);
res.status(err.status || 500).json({ error: err.message || 'Internal Server Error' });
};
16 changes: 16 additions & 0 deletions middlewares/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import multer from 'multer';
import path from 'path';
import fs from 'fs';

const uploadDir = 'uploads/';

if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir);
}

const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, uploadDir),
filename: (req, file, cb) => cb(null, Date.now() + path.extname(file.originalname)),
});

export const upload = multer({ storage });
7 changes: 7 additions & 0 deletions middlewares/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const validate = (struct) => (req, res, next) => {
const [error] = struct.validate(req.body);
if (error) {
return res.status(400).json({ error: error.message });
}
next();
};
Loading