node.js 表单提交,显示(需要改成2个node.js)

大佬们好,我现在做了个表单页面,创建数据库并储存用户资料,然后读取并显示。功能都实现了,就一个js。
现在的问题是,如果要改成2个node.js program要怎么弄?
具体的要求是, 第一个node.js 创建sqlite3 数据库,里面有个TABLE用来存表单发送的数据。
第二个node.js启动服务器,接受到表单发送的数据并存到数据库,然后显示出来。

怎么修改才能以2个NODE.JS实现同样的效果,

下面则是我现在以一个index.js实现的code。

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

var app = express();
var sqlite3 = require('sqlite3').verbose();

// persistant file database "myDB".
var db = new sqlite3.Database('myDB');
//set up table
db.serialize(function (){
    db.run('CREATE TABLE IF NOT EXISTS users (name text, password text, option text);')
});

//now any files in public are routed
app.use(express.static('public'));

//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));

// REST endpoint for posting a new user
app.post('/users', function (req, res, next) {

    let username = req.body.name;
    let password = req.body.password;

    let optradio = req.body.optradio;

    var stmt = db.run(
        `INSERT OR IGNORE INTO users VALUES ("${username}", "${password}", "${optradio}")`

    );
    res.status(200).redirect('/');  
    console.log("Just received POST data for users endpoint!");
    console.log(`Data includes: ${username}, ${password} and ${optradio}`);


});




// REST endpoint for getting all user data
app.get('/users', function (req, res) {

    // Display a web page table
    res.write('<html><body>');
    res.write("<h3> The User Information Table </h3>");
    res.write("<table><tr style='background-color:#006633'>");
    res.write('<th width="150" style="color:#ffff99">Name</th>');
    res.write('<th width="150" style="color:#ffff99">Password</th>');
    res.write('<th width="150" style="color:#ffff99">Option</th><tr>');

    // Retrieve data from table User on the server 
    // and display it in a web page table structure
    db.all('SELECT * FROM users', function(err, rows){
        rows.forEach(function (row){
            res.write('<tr style="background-color:#ccffcc">');
            res.write('<td width="150" align="center">'+row.name+'</td>');
            res.write('<td width="150" align="center">'+row.password+'</td>');
            res.write('<td width="150" align="center">'+row.option+'</td></tr>');
        });
        res.write('</table>');
        res.write('</body></html>');
        res.send();
    });
});

// create a Node.js server that listens on port 3000.
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
})

下面是html

 <!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Node Express</title>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container">
            <h3> My First Express Server... </h3>
            <form action="/users" method="post">
                <div class="form-group">
                    <label for="usr">Name:</label>
                    <input type="text" class="form-control" name="name" id="usr">
                </div>
                <div class="form-group">
                    <label for="pwd">Password:</label>
                    <input type="password" class="form-control" name="password" id="pwd">
                </div>


                <label class="radio-inline"><input type="radio" name="optradio" value="1">Option 1</label>
                <label class="radio-inline"><input type="radio" name="optradio" value="2">Option 2</label>
                <label class="radio-inline"><input type="radio" name="optradio" value="3">Option 3</label>
                <br><br>

                <button type="submit" value="Create Profile" class="btn btn-primary">Submit</button>
            </form>
            <br>
            <form action="/users" method="get">
                <button type="submit" value="Get Users" class="btn btn-primary">Get Users</button>
            </form>
        </div>
    </body>
</html>

3个回答

是要分开为两个js文件吧?建立db.js如下:

 exports.initDB = function() {
    var sqlite3 = require('sqlite3').verbose();

    var db = new sqlite3.Database('myDB');
    //set up table
    db.serialize(function (){
        db.run('CREATE TABLE IF NOT EXISTS users (name text, password text, option text);')
    });
    return db;
}

index.js文件前几行改为:

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

var app = express();

var dbModule = require('./db');
var db = dbModule.initDB();

//now any files in public are routed
app.use(express.static('public'));

一个做成路由,一个做成基础的路由实现啊

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐