Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple form node js application

Tags:

node.js

I'm trying to make a simple form, with user name and last name, and when the user submit information, another page is displayed. I did a form in html, but I'm not sure about what to do next? Does anyone have a small, self-contained example of a form, using node js?

like image 947
Simon Lickus Avatar asked Jul 20 '13 12:07

Simon Lickus


People also ask

How do you create a simple express JS application?

Step 1: Write this command in your terminal, to create a nodejs application, because our express server will work inside the node application. This will ask you for few configurations about your project you can fill them accordingly, also you can change it later from the package. json file.

How do I get Node.js form data?

To get started with forms, we will first install the body-parser(for parsing JSON and url-encoded data) and multer(for parsing multipart/form data) middleware. var express = require('express'); var bodyParser = require('body-parser'); var multer = require('multer'); var upload = multer(); var app = express(); app.


1 Answers

This example does not quite complete your task. But it is a self contained node.js program that displays a form and a different page upon form receipt.

Copy it into a file and then run node filename.js and then go to http://localhost:3000 in a browser.

Take note of the asynchronous code structure. I define a handler function but don't execute it immediately. We instead pass the function to http.createServer and then call .listen(3000). Now when an HTTP request comes in, the http server will pass a req, res pair to the handler function. req is the request object (this will contain the form data; see this question for some hints on how to get that data out. (I suggest that you jump right in and build a small Express app. It's a really nice framework.)

//app.js
// Load the built in 'http' library
var http = require('http'); 
var util = require('util');

// Create a function to handle every HTTP request
function handler(req, res){
  if(req.method == "GET"){ 
    console.log('get');
    res.setHeader('Content-Type', 'text/html');
    res.writeHead(200);
    res.end("<html><body><form action='/' method='post'><input type='text' name='hello'><input type='submit'></form></body></html>");
  } else if(req.method == 'POST'){
    console.log('post');
    // Here you could use a library to extract the form content
    // The Express.js web framework helpfully does just that
    // For simplicity's sake we will always respond with 'hello world' here
    // var hello = req.body.hello;
    var hello = 'world';
    res.setHeader('Content-Type', 'text/html');
    res.writeHead(200);
    res.end("<html><body><h1>Hello "+hello+"!</h1></body></html>");
  } else {
    res.writeHead(200);
    res.end();
  };
};

// Create a server that invokes the `handler` function upon receiving a request
// And have that server start listening for HTTP requests
// The callback function is executed at some time in the future, when the server
// is done with its long-running task (setting up the network and port etc)
http.createServer(handler).listen(3000, function(err){
  if(err){
    console.log('Error starting http server');
  } else {
    console.log('Server listening on port 3000');
  };
});
like image 185
Plato Avatar answered Sep 23 '22 22:09

Plato