I can not get a POST with express (NodeJS)

2

I started to develop a small cahtbot with IBM technology (Watson), I'm using NodeJS for the development part of the application. I am using Express as a routing framework, I have also divided the project into views, routes and the server. I found the following error, I have my route as we can see:

    const express = require('express');
    const router = express.Router();

        router.post('/conversation/', (req, res) => {
          const { text, context = {} } = req.body;

          const params = {
            input: { text },
            workspace_id:'Workspace-id',
            context,
          };

          assistant.message(params, (err, response) => {
            if (err) res.status(500).json(err);

            res.json(response);
          });
        });

module.exports = router;

My Server is in the following way:

/*eslint-env node*/
'use strict';
// This application uses express as its web server
// for more info, see: http://expressjs.com
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');


// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
const cfenv = require('cfenv');
// create a new express server
const app = express();

const AssistantV1 = require('watson-developer-cloud/assistant/v1');

const assistant = new AssistantV1({
  username: 'username',
  password: 'pass',
  url: 'https://gateway.watsonplatform.net/assistant/api/',
  version: '2018-02-16',
});

//Configuring the environment
//Use body-parser to nodejs understand the user requests
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//establish the views directory
app.set('views',path.join(__dirname,'views'));  //__dirname get the main tab project
//establish the views engine
app.set('view engine','ejs');

// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));

// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

//Routes
app.use('/', require('./routes/index'));


// start server on the specified port and binding host
app.listen(appEnv.port, appEnv.bind, () => {
  // print a message when the server starts listening
  console.log("server starting on " + appEnv.url);

});

Now what I want is to start my server in the link link

When I enter it from the browser, I get the following error:

I'm new with NodeJS so, I do not know what I'm doing wrong, the weird thing is that with the GET method I do not have the same problem and if you send me the render of the index.ejs.

    
asked by Roger 28.08.2018 в 22:40
source

1 answer

2

I was reading your code. When accessing that route through the browser, what is happening is that a GET is searched for, not a POST like the one you have. To make POST, GET, DELETE, PUT requests etc. with parameters and experience more things I recommend you use the software POSTMAN it has helped me a lot.

Even if you want to make requests using the URL using GET, you will have to define a new route router.get ('/ conversation /'...

Or make a POST request in javascript, or using a FORM:

var xhr = new XMLHttpRequest();
xhr.open("POST", "tu_url");
xhr.send(parametros);
    
answered by 28.08.2018 / 23:30
source