Skip to content

Instantly share code, notes, and snippets.

@yemmyharry
Last active May 11, 2021 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemmyharry/33a673e13fb0581f68f719a7e478faf5 to your computer and use it in GitHub Desktop.
Save yemmyharry/33a673e13fb0581f68f719a7e478faf5 to your computer and use it in GitHub Desktop.
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