var express = require('express'); var router = express.Router(); const multer = require('multer'); const Teacher = require('../model/teacher_register'); const fs = require('fs') const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uploads/'); // Store uploaded images in the 'uploads' directory }, filename: function (req, file, cb) { cb(null, Date.now() + '-' + file.originalname); }, }); const upload = multer({ storage: storage }); const TeacherRegister = async (req, res, next) => { try { const teacher = new Teacher({ tea_fname: req.body.tea_fname, tea_lname: req.body.tea_lname, tea_email: req.body.tea_email, tea_number: req.body.tea_number, tea_address: req.body.tea_address, tea_subject: req.body.tea_subject, tea_image: req.file.filename, }) const teacher_data = await teacher.save(); let teacher_id = teacher_data._id; res.json({ status: 200, message: "Teacher Register Successfully", teacher_id: teacher_id, destination: "Please Save your Teacher id Becoses login time is required" }) } catch (error) { fs.unlinkSync(req.file.path); res.json({ status: 400, message: "Please Rechake Your Details", hint: "Some information is Duplicate" }) } } router.post("/register",upload.single('tea_image'), TeacherRegister);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter