Nodemon:节点未运行

问题描述 投票:0回答:2

我用server.js文件创建了我的第一个节点应用程序。

当我这样做:nodemon ./server.js localhost 3000

我收到这些消息,但http://localhost:3000上的网站没有运行。

[nodemon] 1.18.10 [nodemon]随时重启,输入rs

[nodemon]观看:。 [nodemon]启动`node ./server.js localhost

3000` [nodemon] clean exit - 在重启前等待更改

我究竟做错了什么?

server.js文件:

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)
        }
    });
});

我想通过邮寄请求来测试邮递员,但我得到:Could not get any response

node.js nodemon
2个回答
1
投票

您需要在代码中添加app.listen行,以便程序保持运行并侦听请求。当前节点只运行该文件,因为在文件末尾没有任何保持程序运行的程序,程序立即退出。

您可以在文件底部添加这样的行:

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

然后只需运行该文件:

nodemon ./server.js

1
投票

您的server.js文件结束,因此您从节点获得一个干净的退出消息。您需要做的是在server.js的末尾添加以下代码行。您需要应用程序始终侦听端口3000.我正在发布所有server.js文件。

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)
    }
  });
});

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

现在,您只能使用nodemon server.js运行代码

© www.soinside.com 2019 - 2024. All rights reserved.