NodeJs操作mysql数据库(coffee版)
来源:互联网 发布:q币拦截软件 编辑:程序博客网 时间:2024/05/01 15:24
1、需要下载mysql模块
http = require('http');
work = require('./lib/timetrack');
mysql = require('mysql');
db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
database: 'nodedb'
});
server = http.createServer((req, res) ->
switch req.method
when 'POST'
switch req.url
when '/'
work.add(db, req, res);
when '/archive'
work.archive(db, req, res);
when '/delete'
work.delete(db, req, res);
when 'GET'
switch req.url
when '/'
work.show(db, res);
when '/archived'
work.showArchived(db, res);
)
db.query(
"""
create table if not exists work(
id int(10) not null auto_increment,
hours decimal(5,2) default 0,
date DATE,
archived int(1) default 0,
description longtext,
primary key(id))
""",
(err) ->
if (err)
throw err
console.log('Server started...')
server.listen(3000, '127.0.0.1')
)
qs = require('querystring')
sendHtml = (res, html) ->
res.setHeader('Content-Type', 'text/html')
res.setHeader('Content-Length', Buffer.byteLength(html))
res.end(html)
parseReceivedData = (req, cb) ->
body = ''
req.setEncoding('utf8')
req.on('data', (chunk) ->
body += chunk
)
req.on('end', ()->
data = qs.parse(body)
cb(data)
)
actionForm = (id, path, label) ->
html = '<form method="POST" action="' + path + '">' +
'<input type="hidden" name="id" value="' + id + '"/>' +
'<input type="submit" value="' + label + '"/>' +
'</form>';
return html
add = (db, req, res) ->
parseReceivedData(req, (work) ->
db.query(
"insert into work(hours,date,description) " +
"values(?,?,?)",
[work.hours, work.date, work.description],
(err) ->
if (err)
throw err
show(db, res)
)
)
exports.delete = (db, req, res) ->
parseReceivedData(req, (work) ->
db.query(
"delete from work where id=?",
[work.id],
(err) ->
if (err)
throw err
show(db, res)
)
)
archive = (db, req, res)->
parseReceivedData(req, (work) ->
db.query(
"update work set archived=1 where id=?",
[work.id],
(err)->
if (err)
throw err
show(db, res)
)
)
show = (db, res, showArchived)->
query = "select * from work where archived=? order by date desc ";
archiveValue = showArchived ? 1: 0;
db.query(
query,
[archiveValue],
(err, rows) ->
if (err)
throw err
html = showArchived ? '': '<a href ="/archived">Archived work</a><br/>';
html += exports.workHitlistHtml(rows)
html += exports.workFormHtml()
)
showArchived = (db, res)->
show(db, res, true);
workHitlistHtml = (rows) ->
html = '<table>';
console.log(rows)
for i in rows
html += '<tr>';
html += '<td>' + rows[i].date + '</td>';
html += '<td>' + rows[i].hours + '</td>';
html += '<td>' + rows[i].description + '</td>';
if !rows[i].archived
html += '<td>' + exports.workArchiveForm(rows[i].id) + '</td>';
html += '<td>' + exports.workDeleteForm(rows[i].id) + '</td>';
html += '</tr>';
html += '</table>';
return html;
workFormHtml = () ->
html = '<form method="POST" action="/"' +
'<p>Date(YYYY-MM-DD):<br/><input name="date" type="text"></p>' +
'<p>Hours worked:<br/><input name="hours" type="text"></p>' +
'<p>Description:<br>' +
'<textarea name="description"></textarea></p>' +
'<input type="submit" value="Add"/>' +
'</form>';
return html;
workArchiveForm = (id) ->
return exports.actionForm(id, '/archive', 'Archive');
workDeleteForm = (id) ->
return exports.actionForm(id, '/delete', 'Delete');
转载来源:http://www.16boke.com/article/detail/121 0 0
- NodeJs操作mysql数据库(coffee版)
- Nodejs 操作mysql数据库
- nodejs操作mysql数据库
- nodejs操作mysql数据库
- nodejs操作mysql数据库
- NodeJs操作mysql数据库(js版)
- NodeJS操作MySQL数据库教程
- nodejs 操作mysql数据库(最新版本)
- nodejs中操作mysql数据库示例
- nodejs简单应用四(数据库mysql操作)
- Nodejs mysql 数据库增、删、改、查 操作
- Nodejs 操作redis数据库
- nodejs sqlite 数据库操作
- nodejs操作mongodb数据库
- nodejs数据库操作
- nodejs操作mongodb数据库
- nodejs操作mongodb数据库
- nodejs - mysql操作示例
- 我使用过的Linux命令之exit - 退出当前shell
- epoll 再探--原理详解
- Crontab定时任务
- Lintcode—落单的数
- rhel 6.7简单安装postgresql 9.2.10
- NodeJs操作mysql数据库(coffee版)
- Android自定义类似ProgressDialog效果的Dialog
- iOS自动生成图片@1x,@2x ,@3x图片
- Codeforces Round #346 (Div. 2) C模拟
- iOS 去掉tableViewCell 点击效果 三个简单方法
- 大型网站之存储瓶颈(数据库的水平扩展)
- C#中偏移量和长度超出数组的界限的问题
- 免责声明
- 修改android默认锁屏方式