我完全是node.js的新手,我想用express和postgresql创建一个简单的电话簿应用程序。我想要有两个页面,一个用于添加新联系人,另一个用于在HTML表格中显示联系人,并能够更新或删除行。到目前为止,我已经实现了插入,但我不知道如何从数据库动态创建“contacts.html”页面。提前谢谢!
index.html
<header>
<ul>
<li><h2>Phonebook</h2></li>
<li><a href="index.html" id="index">New Contact</a></li>
<li><a href="contacts.html" id="contacts">Contacts</a></li>
</ul>
</header>
<section>
<form action="insertContact">
<p>Full Name</p>
<input type="text" name="fullname" required>
<p>Phone</p>
<input type="text" name="phone1" required>
<p>Mobile</p>
<input type="text" name="phone2">
<p>Address</p>
<input type="text" name="address" required> <br><br>
<input type="submit" name="submitBtn" id="submitBtn" value="Submit">
</form>
</section>
server.js
var express = require('express');
var path = require('path');
var db = require('pg');
var http = require('http');
var app = express();
app.use(express.static(path.join(__dirname,'/')));
var dbConnection = "postgres://postgres:root@localhost:5432/Phonebook";
app.get('/insertContact',function(req,res){
var dbClient = new db.Client(dbConnection);
dbClient.connect(function(err){
if(err)
throw err;
var query = "insert into Contacts (fullname,phone,mobile,address) values ($1,$2,$3,$4)";
var fullname = req.query.fullname;
var phone = req.query.phone1;
var mobile = req.query.phone2;
var address = req.query.address;
var contact = [fullname , phone , mobile , address];
dbClient.query(query , contact , function(err){
if(err)
throw err;
else {
console.log('Success!') ;
res.redirect('/');
res.end();
}
});
});
});
app.get('????',function(req,res) {
var dbClient = new db.Client(dbConnection);
dbClient.connect(function(err){
if(err)
throw err;
var query = "select * from Contacts";
dbClient.query(query,function(err,result){
if(err)
throw err;
else {
??????????
res.end();
}
});
});
});
app.listen(8080,function(){
console.log('Server started');
});