私とNodeJS(十二)ウェブフレームを勉強します.

6435 ワード

設置:node_にありますmodulesディレクトリ
D:\NodeJs>cnpm install express --save
√ Installed 1 packages
√ Linked 46 latest versions
√ Run 0 scripts
Recently updated (since 2018-02-15): 4 packages (detail see file D:\NodeJs
ode_modules\.recently_updates.txt) √ All packages installed (50 packages installed from npm registry, used 4s, speed 41.7kB/s, json 47(103.8kB), tarball 70.47kB) D:\NodeJs>
httpのget/post要求:

//  express web  
var express = require('express');
//    events   
var events = require('events');

var bodyParser = require('body-parser');

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

var application = express();

application.get('/',function(request,response){
    response.send('test~~~~');
});

application.get('/list',function(request,response){
    console.log("  list.html GET  ");
    response.send('  list.html  ~~~~');
});

application.get('/login.html',function(request,response){
    console.log("  login.html GET  ");
    response.sendFile(__dirname + "/" + "./login.html");
});

application.post('/login_post',urlencodedParser,function(request,response){
    console.log("  login.html POST  ");
    //JSON    
    var data ={"username":request.body.username,"password":request.body.password};
    console.log(data);
    response.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
    response.end(JSON.stringify(data));
    response.send('  login.html  ~~~~');
});

var server = application.listen(8888,function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log("      http://%s:%s", host, port);
});
login.

<html>
<head>
    <meta charset="UTF-8">
    <title>login  title>
head>
<body>
<form action="http://127.0.0.1:8888/login_post" method="POST">
username: <input type="text" name="username">  <br/>
password: <input type="password" name="password"> <br/>
<input type="submit" value="Submit">
form>
body>
html>