简体   繁体   中英

nodemon_app crashed - waiting for file changes before starting

1.This is my productController:

const Product = require('../models/product')
//Create 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 show all produts in database.'

    })
}
  1. This is 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"
    }
}
  1. This is my server.js file
const app = require('./app');
const connectDatabase = require('./config/database')
const dotenv = require('dotenv');
//Setting up config files
dotenv.config({ path: 'backend/config/config.env' })
//Connecting to database
connectDatabase();
app.listen(process.env.PORT, () => {
    console.log(`Server started on PORT: ${process.env.PORT} in ${process.env.NODE_ENV} 
mode.`)
});
  1. This is 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;

This is my 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...

you should remove the colon from the newProduct function like so

exports.newProduct = async (req, res, next) => {
  const product = await Product.create(req.body);
  res.status(201).json({
    success: true,
    product 
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM