无法发布 /api/sentiment

tza00

我正在邮递员中测试 /api/sentiment 的端点,但我不确定为什么会收到 cannot POST 错误。我相信我传递了正确的路由并且服务器正在侦听端口 8080。所有其他端点都运行没有问题,所以我不确定是什么导致了这里的错误。

server.js 文件

const express = require("express");
const cors = require("cors");
const dbConfig = require("./app/config/db.config");

const app = express();

var corsOptions = {
  origin: "http://localhost:8081"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(express.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));

const db = require("./app/models");
const Role = db.role;

db.mongoose
  .connect(`mongodb+srv://tami00:[email protected]/test?retryWrites=true&w=majority`, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then(() => {
    console.log("Successfully connect to MongoDB.");
    initial();
  })
  .catch(err => {
    console.error("Connection error", err);
    process.exit();
  });

// simple route
app.use('/api/favourite', require('./app/routes/favourite.routes'));
app.use('/api/review', require('./app/routes/review.routes'));
app.use('/api/sentiment', require('./app/routes/sentiment-analysis.routes'));
// routes
// require(".app/routes/favourite.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/user.routes")(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

function initial() {
  Role.estimatedDocumentCount((err, count) => {
    if (!err && count === 0) {
      new Role({
        name: "user"
      }).save(err => {
        if (err) {
          console.log("error", err);
        }

        console.log("added 'user' to roles collection");
      });

      new Role({
        name: "creator"
      }).save(err => {
        if (err) {
          console.log("error", err);
        }

        console.log("added 'creator' to roles collection");
      });

      new Role({
        name: "watcher"
      }).save(err => {
        if (err) {
          console.log("error", err);
        }

        console.log("added 'watcher' to roles collection");
      });
    }
  });
}

情绪分析路线文件

const express =  require('express');
const router = express.Router();
const getSentiment = require('../sentiment-analysis/sentimentAnalysis')

router.post('/api/sentiment', (req, res) => {
    const data = req.body.data
    const sentiment = getSentiment(data)
    return res.send({sentiment})
})

module.exports = router;

情绪分析.js 文件

const aposToLexForm = require("apos-to-lex-form");
const {WordTokenizer, SentimentAnalyzer, PorterStemmer} =  require("natural");
const SpellCorrector = require("spelling-corrector");
const stopword =  require("stopword");

const tokenizer = new WordTokenizer();
const spellCorrector = new SpellCorrector();
spellCorrector.loadDictionary();

const analyzer = new SentimentAnalyzer('English', PorterStemmer, 'afinn')

function getSentiment(text){
    if(!text.trim()) {
        return 0;
    }

    const lexed = aposToLexForm(text).toLowerCase().replace(/[^a-zA-Z\s]+/g, "");

    const tokenized = tokenizer.tokenize(lexed)
    
    const correctSpelling = tokenized.map((word) => spellCorrector.correct(word))

    const stopWordsRemoved =  stopword.removeStopwords(correctSpelling)

    console.log(stopWordsRemoved)

    const analyzed = analyzer.getSentiment(stopWordsRemoved);

    console.log(analyzed)

}

module.exports = getSentiment;

console.log(getSentiment("Wow this is fantaztic!"))
console.log(getSentiment("let's go together?"))
console.log(getSentiment("this is so bad, I hate it, it sucks!"))
瓦西利斯

我看到您使用的路线如下:app.use('/api/sentiment', require('./app/routes/sentiment-analysis.routes'));。但是在你的情绪分析中你再次使用 /api/sentiment 所以你的请求 URL 应该是 /api/sentiment/api/sentiment

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章