// this is my index.js pubblished on vercel
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config({path: __dirname+'/.env'});
}
const express = require("express");
const multer = require("multer");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const authRoute = require("./routes/auth");
const userRoute = require("./routes/users");
const postRoute = require("./routes/posts");
const categoryRoute = require("./routes/categories");
const path = require("path");
const app = express();
dotenv.config();
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: false,
})
.then(() => {
console.log("MongoDB connected!");
})
.catch((err) => {
console.log(err);
});
app.use(express.json());
app.use("/images", express.static(path.join(__dirname, "/images")));
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(null, req.body.name);
},
});
const upload = multer({ storage: storage });
app.post("/api/upload", upload.single("file"), (req, res) => {
res.status(200).json("File has been uploaded");
});
//endpoints
app.use("/api/auth", authRoute);
app.use("/api/users", userRoute);
app.use("/api/posts", postRoute);
app.use("/api/categories", categoryRoute);
app.get('/', (req,res)=>{
res.json({
"ciao":"io sono il backend",
});
});
// static files (build of your frontend)
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../frontend', 'build')));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend', 'build', 'index.html'));
})
}
app.listen(process.env.PORT || 5000, () => {
console.log("Listening at port 5000");
});
I created a Mern blog with 2 folders: one client (the front end part) and api (the back end part). I would like to distribute in a free hosting, but I don't want to use Heroku, because after November 22nd it will be paid, How can I do this deployment without Heroku in the server part, can someone clarify my ideas on the procedure to do? do i have to merge the two folders?