Search code examples
node.jsnodemon

Nodemon : node not running


I've create my first node application with a server.js file.

When I make this : nodemon ./server.js localhost 3000

I get these messages but the website on http://localhost:3000 is not running.

[nodemon] 1.18.10 [nodemon] to restart at any time, enter rs

[nodemon] watching: . [nodemon] starting `node ./server.js localhost

3000` [nodemon] clean exit - waiting for changes before restart

What am I doing wrong?

server.js file :

const express = require('express');
const app = express();
const multipart = require('connect-multiparty');
const cloudinary = require('cloudinary');
const cors = require('cors');
const bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());

const multipartMiddleware = multipart();

cloudinary.config({
    cloud_name: 'aaa',
    api_key: 'xxx',
    api_secret: 'xxxxxdd'
});

app.post('/upload', multipartMiddleware, function(req, res) {
  cloudinary.v2.uploader.upload(req.files.image.path,
    {
      ocr: "adv_ocr"
    }, function(error, result) {
        if( result.info.ocr.adv_ocr.status === "complete" ) {
          res.json(result); // result.info.ocr.adv_ocr.data[0].textAnnotations[0].description (more specific)
        }
    });
});

I'm trying to test with postman by make a post request but I get : Could not get any response


Solution

  • You need to add an app.listen line to your code so that the program stays running and listening for requests. Currently node just runs the file and since there is nothing keeping the program running by the end of the file, the program exits immediately.

    You can add a line like this to the bottom of your file:

    const port = 3000;
    app.listen(port, () => console.log(`Listening for requests on port ${port}`));
    

    Then just run the file with:

    nodemon ./server.js