Home > database >  Cannot POST /api/sentiment
Cannot POST /api/sentiment

Time:04-10

I'm testing the endpoint for /api/sentiment in postman and I'm not sure why I am getting the cannot POST error. I believe I'm passing the correct routes and the server is listening on port 8080. All the other endpoints run with no issue so I'm unsure what is causing the error here.

server.js file

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

sentiment-analysis routes file

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;

sentimentAnalysis.js file

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!"))

CodePudding user response:

Shouldn't it be:

 const data = req.body.data

CodePudding user response:

I see that you use your routes like: app.use('/api/sentiment', require('./app/routes/sentiment-analysis.routes'));. But then in your sentiment-analysis you again use /api/sentiment so your request URL should be /api/sentiment/api/sentiment

  • Related