Node.js server doesn't receive html post requests


#1

Hi, I’ve been learning Node.js using Express, I tried to make a simple HTML post request, that works on my local system, but doesn’t on Cloud9.

Heres my server.js:

var express = require(‘express’);
var app = express();
var bodyParser = require(‘body-parser’);

// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.get(’/index.htm’, function (req, res) {
res.sendFile( __dirname + “/” + “index.htm” );
})

app.post(’/process_post’, urlencodedParser, function (req, res) {
// Prepare output in JSON format
response = {
first_name:req.body.first_name,
last_name:req.body.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})

var server = app.listen(process.env.PORT, process.env.IP, function () {
var host = server.address().address
var port = server.address().port

console.log(“Example app listening at https://%s:%s”, host, port)

index.htm:

  <form action = "https://$IP:$PORT/process_post" method = "POST">
     First Name: <input type = "text" name = "first_name"> <br>
     Last Name: <input type = "text" name = "last_name">
     <input type = "submit" value = "Submit">
  </form>

I’ve already tried running it using http instead of https and my application url is public. When I run this code in my local system it works, when click “submit” I go to the “process_post” page and “first_name” and “last_name” are printed on the console, but when run on cloud9 I get an “about:blank” page and nothing on the console.


#2

is $IP:$PORT in action url, your actual code? I think you need to write the url there
Also what errors do you see in browser console?


#3

Hi, I tried changing “$IP:$PORT” to “https://magic-collection-manager-bfbonatto.c9users.io:8080”, now instead of a blank page I get sent back to https://magic-collection-manager-bfbonatto.c9users.io:8080 when I click submit. No console errors or messages at all.


#4

i believe you need to use action="https://magic-collection-manager-bfbonatto.c9users.io/process_post" or yet better just action=/process_post"


#5

It worked, thank you very much!