需要把
views文件中的子文件全部改为以 .ejs的后缀
1·npm install express -g (全局安装)
2·npm install -g express-generator (安装命令工具)
3·express myapp(新建的项目名称--myapp)
4·cd 项目名称
5·npm install (安装项目依赖)
6. npm start(--项目启动) 需要在package.json文件中"scripts"这个对象中配置启动命令的【路径】--
"start": "node ./bin/www"
需要命令行安装插件:
body-parser
cookie-parser
morgan
mysql
orm
path
serve-favicon等等插件!!
【重点中的重点:《app.js文件的配置:》】:
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 orm = require('orm');
var index = require('./routes/index');
var users = require('./routes/users');
var demo = require('./routes/demo');
var app = express();
app.use(orm.express("mysql://root:root@127.0.0.1:3306/movie",{
define:function(db,models,next){
next();
}
}))
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
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')));
app.use('/', index);
app.use('/users', users);
app.use('/demo', demo);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
【重点::】【请求数据的方式===例如:get和post方式2个:】:
// 此为 注册页面 的添加
router.post('/login', function(req, res, next) {
var name=req.body.name;
var pwd=req.body.pwd;
var sql="insert into login(user,pwd) values('"+name+"','"+pwd+"')";
req.db.driver.execQuery(sql,function(err,data){
if(err){
res.json({mag:"信息注册失败",flag:false,err:err});
}else{
res.json({mag:"信息注册成功",flag:true,data:data});
}
})
});
// 此为 登录页面 的查询
router.get('/login', function(req, res, next) {
var name=req.body.name;
var pwd=req.body.pwd;
var sql="select * from login where user='"+name+"' and pwd='"+pwd+"')";
req.db.driver.execQuery(sql,function(err,data){
if(err){
res.json({mag:"登录失败",flag:false,err:err});
}else{
res.json({mag:"登录成功",flag:true,data:data});
}
})
});