0

I want to print an input value using restify and post route, but it's not working. The form.html page opens and upon submitting, I see this in URL:

http://localhost:8081/?name=dsf

But I don't see any message in console. Am I doing something wrong?

Here's my code:

select.js

var restify=require('restify');
var server=restify.createServer();
var mysql=require('mysql');
server.listen(8081, function(){
    console.log("%s is running at %s", server.name, server.url);
});
var pool=mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'books'
});
server.get(/\/?.*/, restify.serveStatic({
    directory: __dirname,
    default: 'form.html'
}));
server.post('/hello/', function send(req, res, next){
    pool.getConnection(function(err, connection){
        if(err){
        }else{
            var table=req.body.name;
            console.log(table);
        }
    });
});

form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="POST">
    <input type="text" name="name">
    <input type="submit" value="submit">
</form>
</body>
</html>

Both files are in C:/restify folder.

1
  • To start with, add a console.log statement to print out the err if it exists. Commented Nov 5, 2016 at 5:22

1 Answer 1

1

action attribute is missing in your <form> tag. Try to set it to action="/hello/". Does it solve your problem?

Sign up to request clarification or add additional context in comments.

4 Comments

This was a mistake. I corrected it but still same response.
Here: var table=req.body.name;. Are you sure that it shouldn't be like: var table=res.body.name;? res instead of req
Yes, pretty sure.
I don't know how much you know Restify but look here: restify.com I'm pretty sure that in your server.post() function there is some mismatch. Try removing name of your send funciton. Leave it like this: function(req, res, next)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.