1. My ProductController Implementation:
const Product = require('../models/product')
//Creating a new product => /ap1/v1/product/new
exports.newProduct = async(req, res, next) => {
const product = await Product.create(req.body);
res.status(201).json({
success: true,
product:
})
}
exports.getProducts = (req, res, next) => {
res.status(200).json({
success: true,
message: 'This route will display all products stored in the database.'
})
}
- The content of my package.json file:
{
"name": "shopit",
"version": "1.0.0",
"description": "e-commerce using MERN",
"main": "server.js",
"scripts": {
"start": "node backend/server.js",
"dev": "SET NODE_ENV=DEVELOPMENT& nodemon backend/server",
"prod": "nodemon ./server.js localhost 8080: "
},
"author": "Floyd",
"license": "ISC",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.11.13"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
- This is the server.js setup:
const app = require('./app');
const connectDatabase = require('./config/database')
const dotenv = require('dotenv');
//Configuring environment variables
dotenv.config({ path: 'backend/config/config.env' })
//Connecting to the database
connectDatabase();
app.listen(process.env.PORT, () => {
console.log(`Server running on PORT: ${process.env.PORT} in ${process.env.NODE_ENV}
mode.`)
});
- The content of my routes file:
const express = require('express');
const router = express.Router();
const { getProducts, newProduct } = require('../controllers/productController');
router.route('/products').get(getProducts);
router.route('/product/new').post(newProduct);
module.exports = router;
The encountered error:
D:\PROGRAMMING\2077\SHOPIT\backend\controllers\productController.js:12
})
^
SyntaxError: Unexpected token '}'
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (D:\PROGRAMMING\2077\SHOPIT\backend\routes\product.js:4:37)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
[nodemon] app crashed - waiting for file changes before starting...