Nodemon:节点不是 运行

Nodemon : node not running

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

当我做这个的时候: nodemon ./server.js localhost 3000

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

[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

我做错了什么?

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

我正在尝试通过 post 请求与 postman 进行测试,但我得到:Could not get any response

您的 server.js 文件结束,因此您从 node.What 收到一条干净的退出消息,您需要做的是在 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

您需要在代码中添加 app.listen 行,以便程序保持 运行ning 并侦听请求。当前 node 只是 运行s 文件,并且由于文件末尾没有保留程序 运行ning,程序立即退出。

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

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

然后 运行 文件包含:

nodemon ./server.js