#2 connect multer for file handling

parent 96aa4852
This diff is collapsed.
...@@ -11,13 +11,19 @@ ...@@ -11,13 +11,19 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@types/node": "^18.15.3", "@types/node": "^18.15.3",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"mongoose": "^7.0.1", "mongoose": "^7.0.1",
"multer": "^1.4.5-lts.1",
"ts-node-dev": "^2.0.0" "ts-node-dev": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"@types/mongoose": "^5.11.97" "@types/mongoose": "^5.11.97",
"@types/multer": "^1.4.7"
} }
} }
\ No newline at end of file
import express, {Express, json} from 'express'; import express, {Express, json} from 'express';
import mongoose from 'mongoose';
import 'dotenv/config'; import 'dotenv/config';
import {artistRouter} from './routes/artist'; import {artistRouter} from './routes/artist';
import cors from 'cors';
const app: Express = express(); const app: Express = express();
app.use(json()); app.use(json());
app.use(cors());
app.use('/artists', artistRouter); app.use('/artists', artistRouter);
app.listen(process.env.PORT, () => { app.listen(process.env.PORT, () => {
console.log('Server started on port ' + process.env.PORT); console.log('Server started on port ' + process.env.PORT);
}); });
import express, {Request, Response} from 'express'; import express, {Request, Response} from 'express';
import {model, Schema, connect, Document, HydratedDocument} from 'mongoose'; import {model, Schema, connect, HydratedDocument} from 'mongoose';
import multer from 'multer';
import fs from 'fs';
import path from 'path';
const upload = multer({dest: 'uploads/'});
const router = express.Router(); const router = express.Router();
interface IArtist { interface IArtist {
name: string; name: string;
image?: File; image: File;
description: string; description: string;
} }
const artistSchema = new Schema<IArtist>({ const artistSchema = new Schema<IArtist>({
name: { name: {
type: String, type: String,
required: true, required: true,
}, },
image: { image: {
type: Buffer, data: Buffer,
}, contentType: String,
description: { },
type: String, description: {
required: true, type: String,
}, required: true,
},
}); });
const Artist = model<IArtist>('Artist', artistSchema); const Artist = model<IArtist>('Artist', artistSchema);
const run = async () => { const run = async () => {
await connect(`${process.env.MONGO_URL}/artist`); await connect(`${process.env.MONGO_URL}/artist`);
}; };
run().catch((err) => console.log(err)); run().catch((err) => console.log(err));
router.get('/', (req: Request, res: Response) => { router.get('/', async (req: Request, res: Response) => {
return res.send('Hello'); const artists = await Artist.find();
res.send(artists);
}); });
router.post('/', async (req: Request, res: Response) => { router.post(
const artist: HydratedDocument<IArtist> = new Artist({ '/',
name: req.body.name, upload.single('image'),
description: req.body.description, async (req: Request, res: Response) => {
}); const artist: HydratedDocument<IArtist> = new Artist({
await artist.save(); name: req.body.name,
res.send(artist); description: req.body.description,
}); image: {
data: fs.readFileSync(path.join('uploads/' + req.file?.filename)),
contentType: 'image',
},
});
await artist.save();
res.send('Sucessfully saved');
}
);
export {router as artistRouter}; export {router as artistRouter};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment