1.相关资料
解决跨域
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/
else next();
});
相关依赖
{
"name": "web",
"version": "0.0.0",
"author": "mufeng",
"description": "Nodejs Package json",
"keywords": "javascript, nodejs",
"respository": {
"type": "git",
"url": "http://path/to/url"
},
"bugs": {
"url": "http://path/to/bug",
"email": "bug@example.com"
},
"contributors": [
{
"name": "mufeng",
"email": "mufeng5619@gmail.com"
}
],
"license": "MIT",
"engines": { "node": "0.10.x" },
"script": {
"start": "node start.js"
},
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0",
"mysql": "latest",
"express-session": "latest"
},
"devDependencies": {
"bower": "~1.2.8",
"grunt": "~0.4.1",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-clean": "~0.5.0",
"browserify": "2.36.1",
"grunt-browserify": "~1.3.0"
}
}
弱智一样启动js
var express = require('express');
var app = express();
var http = require('http');
var bodyParse = require('body-parser')
var cookieParser = require('cookie-parser');
var cp = require('child_process');
const path = require('path');
var session = require('express-session');
var mysql = require('mysql');
var dbConfig = require('./models/dbcon');
var userSQL = require('./models/db');
app.use(cookieParser());
app.use(bodyParse.urlencoded({ extended: false }));
// 引入css、images、js等静态文件
app.use(express.static(path.join(__dirname, 'public')));
// 解决跨域
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1')
if (req.method == "OPTIONS") res.send(200); /*让options请求快速返回*/
else next();
});
var pool = mysql.createPool(dbConfig.mysql);
// 响应一个JSON数据
var responseJSON = function(res, ret) {
if (typeof ret === 'undefined') {
res.json({
code: '-200',
msg: '操作失败'
});
} else {
res.json(ret);
}
};
// 添加用户
app.get('/addUser', function(req, res, next) {
// 从连接池获取连接
pool.getConnection(function(err, connection) {
// 获取前台页面传过来的参数
var param = req.query || req.params;
console.log(param);
connection.query(userSQL.insert, [param.username, param.password], function(err, result) {
var all;
connection.query(userSQL.queryAll, function(err, result) {
all = result
console.log(all);
});
console.log(all + "--------------------------");
if (result) {
result = {
code: 200,
msg: '增加成功',
// result: all
};
}
// 以json形式,把操作结果返回给前台页面
responseJSON(res, result);
// 释放连接
connection.release();
});
});
});
// 处理根目录的get请求
app.get('/', function(req, res) {
res.sendfile('public/main.html');
console.log('main page is required ');
});
// 处理根目录的字体请求
app.get('/Tangerine.ttf', function(req, res) {
res.sendfile('Tangerine.ttf');
console.log('font page is required ');
});
// 处理根目录的字体请求
app.get('/favicon.png', function(req, res) {
res.sendfile('favicon.png');
console.log('fav page is required ');
});
app.get('/x', function(req, res) {
res.sendfile('public');
console.log('fav page is required ');
});
// 处理/login的get请求
app.get('/add', function(req, res) {
res.sendfile('public/add.html');
console.log('add page is required ');
});
// 处理/login的post请求
app.post('/login', function(req, res) {
name = req.body.name;
pwd = req.body.pwd;
console.log(res);
console.log(name + '--' + pwd);
if (name === 'admin' && pwd === 'admin') {
res.status(200).send(name + '--' + pwd);
} else {
status = 404;
res.status(404).send(status + 'Not Found');
}
});
// 监听8081端口
var server = app.listen(8082, function() {
var host = server.address().address
var port = server.address().port
cp.exec(`explorer http://localhost:${port}`, function() {});
console.log("应用实例,访问地址为 http://%s:%s", host, port)
});
主要就是这些多有借鉴效果如下