使用Node.js和Express添加,更新或删除注释

时间:2019-02-06 02:47:44

标签: html node.js sqlite express

这是我要解决的Question。 这是我为此创建的app.js文件。但是,我无法提交表格。当select元素选择“ new”并按下按钮时,我正在使用列表添加新笔记。但是,由于我是初学者,所以我不太了解如何使用node.js和进行表达。如果有人可以帮助我解决问题,那将是一个很大的帮助。谢谢!

代码如下:

const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database( __dirname + '/users.db',
    function(err) {
        if ( !err ) {
            db.run(`
                CREATE TABLE IF NOT EXISTS users (
                    id INTEGER PRIMARY KEY,
                    note TEXT,
                    position INTEGER,
                    value INTEGER
            )`);
            console.log('opened users.db');
        }
    });

const express = require('express');
const format = require('string-format');
const bodyParser = require('body-parser');
const app = express();
// registers the middleware used to parse post data
app.use(bodyParser.urlencoded({ extended: true }));

// check the environment for a different port
const port = process.env.PORT || 8000;

const users_page = `\
<html>
<head>
<meta charset="utf-8">
<style>
li {{ margin-top: 5px; }} // escapes the {
</style>
</head>
<body>
<h1>user</h1>
<ul>
{}
<li>
<form action="" method = post>
<select name="note1" id="note1">
    <option value="new">new</option>
    <option value="delete">delete</option>
    <option value="first">first</option>
    <option value="last">last</option>
</select>
<input type=submit name="op" value="Do It">
<textarea name="" id="" value = "" ></textarea>
</form>
</li>
</ul>
</body>
</html>
`

function make_users_page( users ) {
    let rep = '';
    for(let u of users) {
        const id = u.id;
        const note = u.note;
        const passwd = u.passwd;
        const enabled = u.enabled ? 'checked':  '';

        rep += '<form action="" method = post>';
        rep += '<select name="note1" id="note1">';
        rep += '<option value="new">new</option> ';
        rep += ` <option value="delete">delete</option>`;
        rep += `  <option value="first">first</option>`;
        rep += ` <option value="last">last</option>`;
        rep += ' </select>';
        rep += ' <input type=submit name="op" value="Do It">';
        rep += `<textarea value = "${note}">`;
        rep += '</textarea>'
        rep += '</form>';

    }
    return format( users_page, rep );
}

function generate_users_page( res ) {
    db.all('SELECT * FROM users',[], function(err, rows) {
        if ( !err ) {
            res.type('.html');
            res.send( make_users_page( rows ));
        }
    } );
}

// registers GET requests
app.get('/', function(req, res) {
    generate_users_page( res );
});

// registers POST requests
app.post('/', function(req, res) {
    const form = req.body; // body contains the form data

    if ( form.op === 'new' ) {
        console.log('new', form );
        db.run(`INSERT INTO users(note) VALUES(?,)`,
            [form.note],
            function( err) { if (!err) { res.redirect('/'); } }
        );
    }

});

app.listen(port, () => console.log(`Listening on port ${port}!`));

0 个答案:

没有答案