Last active
May 11, 2021 22:48
-
-
Save yemmyharry/33a673e13fb0581f68f719a7e478faf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from "express"; | |
import mongoose from "mongoose"; | |
import cors from "cors"; | |
import Post from "../models/post.js"; | |
const app = express(); | |
app.use(express.json({limit: '25mb'})); | |
app.use(express.urlencoded({limit: '25mb', extended: true})); | |
app.use(cors()); | |
app.use("/uploads", async (req, res, next) => { | |
const body = req.body; | |
try { | |
const newImage = await Post.create(body); | |
newImage.save(); | |
res.status(201).json({message: "new image uploaded", createdPost: newImage}); | |
} catch (error) { | |
res.status(409).json({ | |
message: error.message, | |
}); | |
} | |
};); | |
mongoose.connect('mongodb://localhost/imageUpload', { useUnifiedTopology: true , useNewUrlParser: true}) | |
.then(console.log('database connected')) | |
.catch(err => err) | |
const PORT = 5000; | |
app.listen(PORT, () => { | |
console.log("listening at port " + PORT); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment