progs/App1.js
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 11 Oct 2016 15:14:59 +0100
changeset 477 b2c5a721f360
parent 163 02026ee18378
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
163
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
var express = require('express');
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
var bodyParser = require('body-parser');
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
var app = express();
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
app.use(bodyParser.urlencoded({ extended: true }));
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
// sending the form
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
app.get('/', function(req, res){
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
    var html = '<form action="/" method="post">' +
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
        'Login:    <input type="text" name="login" /><br>' +
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
        'Password: <input type="password" name="pass" /><br>' + 
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
        '<button type="submit">Submit</button>' +
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
        '</form>';
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    res.send(html);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
});
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
// receiving data
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
app.post('/', function(req, res){
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    var html = 'Received login: ' + req.body.login + '<br>' +
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
        'Received password: ' + req.body.pass + '<br>' +
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
        '<a href="/">Try again</a>';
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    res.send(html);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
});
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
// starting the server
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
app.listen(8000);
02026ee18378 added js files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
console.log("Server running at http://127.0.0.1:8000/");