码迷,mamicode.com
首页 > 数据库 > 详细

Node.js开发 ---- nodejs操作mysql

时间:2017-01-20 14:32:26      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:工具   cat   pat   name   hand   操作   data   ogg   get   

app.js
[html] view plain copy
//初始化包
var express = require(‘express‘);
var path = require(‘path‘);
var favicon = require(‘serve-favicon‘);
var logger = require(‘morgan‘);
var cookieParser = require(‘cookie-parser‘);
var bodyParser = require(‘body-parser‘);
var mysql = require("mysql");
var jade = require(‘jade‘);
var routes = require(‘./routes/index‘);

var app = express();

//加载引擎模板
app.set(‘view engine‘, ‘jade‘);
app.set(‘views‘, path.join(__dirname, ‘views‘));

//调用工具
app.use(logger(‘dev‘));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, ‘public‘)));

routes(app);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error(‘Not Found‘);
err.status = 404;
next(err);
});

// will print stacktrace
if (app.get(‘env‘) === ‘development‘) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render(‘error‘, {
message: err.message,
error: err
});
});
}

// no stacktraces leaked to users
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render(‘error‘, {
message: err.message,
error: {}
});
});

app.listen(3000);

[html] view plain copy 在CODE上查看代码片派生到我的代码片
var user = require("../models/user.js");

module.exports = function (app) {
app.post(‘/updateUsers‘, function (req, res, next) {
user.updateUsers({
sql: "update user SET username=?,password=?,age=?,info=? WHERE id = ?",
params: [ req.body.username, req.body.password, req.body.age, req.body.info, req.body.id ]
}, function (data) {
res.send(data);
});
});

app.post(‘/deleteUsers‘, function (req, res, next) {
user.deleteUsers({
sql: "DELETE FROM user WHERE id = " + req.body.id
}, function (data) {
res.send(data);
});
});

app.post(‘/addUsers‘, function (req, res, next) {
user.addUsers({
data: {
username: req.body.username,
password: req.body.password,
age: req.body.age,
info: req.body.info
},
sql: "INSERT INTO user SET ?"
}, function (data) {
res.send(data);
});
});
};

Node.js开发 ---- nodejs操作mysql

标签:工具   cat   pat   name   hand   操作   data   ogg   get   

原文地址:http://www.cnblogs.com/flower46273736/p/6322517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!