Shilpa
Shilpa

Reputation: 98

Getting a HTTP 404 error when doing app.post in Node.js

I am newbie to Node.js trying to learn through tutorials found online using Netbeans.

When I do: http://localhost:9080/ I see the Date and the color as expected. But when I try to do http://localhost:9080/add, to see the app.post part, I get HTTP 404 error.

Could anyone let me know what am i doing wrong.

Thanks in advance,

ind.ejs

  <!doctype html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
      <h1> my app </h1>
        <%= new Date() %>
        Color is: 
     <%= test %>
  </body>
  </html>

index.js:

'use strict';

module.exports = require('./lib/express');

var http = require('http');
var express = require('express');
var path = require('path');
var ejs = require('ejs');
var app = express();
var tropo_webapi = require('tropo-webapi');
var bodyParser = require('body-parser');

var test;

var app = express();

//app.use(bodyParser());

app.set('view engine','ejs');
app.set('views', path.join(__dirname,'views'));

app.get('/',function(req,res){
    var test = 'red';
    console.log('test in get is :' + test);
    res.render('ind.ejs',{test:test});
});

app.post('/add',function(req,res){
     test = 'blue';
    console.log('test in post is :' + test);
    res.render('ind.ejs',{test:test});
});

app.listen(9080, function(){
    console.log('Ready on port 9080');
});

Upvotes: 0

Views: 1120

Answers (1)

furydevoid
furydevoid

Reputation: 1401

app.get is for the 'GET' http verb, which is used by default. app.post is triggered for the 'POST' http verb, which can be done using forms:

<form action="/add" method="post"><button type="submit">go</button></form>

Upvotes: 4

Related Questions