返回列表

express base64 to img/image

默认分类 2017/03/16 02:08

代码如下:

'use strict';

var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var Formidable = require('formidable');
var Session = require('express-session');
var UUID = require('uuid');

var app = express();
var port = 80;

app.use(cookieParser());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'));

app.use(Session({
  genid: function(req) {
    return UUID.v1(); // use UUIDs for session IDs
  },
  cookie: {
    maxAge: 60000
  },
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}));

app.use('/session', function(req, res, next) {
  var sess = req.session;
  if (sess.views) {
    sess.views++;
    res.setHeader('Content-Type', 'text/html');
    res.write('<p>views: ' + sess.views + '</p>');
    res.write('<p>expires in: ' + (sess.cookie.maxAge / 1000) + 's</p>');
    res.end();
  } else {
    sess.views = 1;
    res.end('welcome to the session demo. refresh!');
  }
});

app.all('*', function(req, res, next) {
  console.log('req.url: ' + req.url);
  next();
});
app.all('/?', function(req, res) {
  res.send('Hi');
});
app.all('/hi', function(req, res) {
  res.send('Hi world');
});

app.options('/cross_domain', function(req, res) {
  res.setHeader('Access-Control-Allow-Method', 'POST,OPTIONS,GET');
  res.setHeader('Access-Control-Allow-Origin', 'http://a.com');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With')

  // res.send('{success:1}');
  res.status(204);
  res.end();
});

app.all('/cross_domain', function(req, res) {
  res.setHeader('Access-Control-Allow-Method', 'POST,OPTIONS,GET');
  res.setHeader('Access-Control-Allow-Origin', 'http://a.com');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With')
  res.setHeader('X-Power', 'kkkk')

  res.send('{success:1}');
});

app.all('/login', function(req, res) {
  res.redirect('http://b.com/cross_domain');
});

app.all('/test/:id', function(req, res) {
  var result = {
    cookie: req.cookie,
    params: req.params,
    query: req.query,
    body: req.body
  };

  res.send(result);
});

app.post('/file', function(req, res) {
  var form = new Formidable.IncomingForm();
  form.uploadDir = './public';
  form.keepExtensions = true;

  form.parse(req, function(err, fields, files) {
    console.log(fields);
    console.log(files);

    res.send('ok');
  });
});

app.use('/base64img', function(req, res, next) {
  res.writeHead('200', {
      'Content-Type': 'image/png'
  }); //写http头部信息
  res.end(new Buffer('iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0', 'base64'), 'binary');
});

var server = app.listen(port, function() {
  console.log('Example app listening at http://0.0.0.0:%s', port);
});

在app.js同级目录新建一个叫public的文件夹;

别忘了 node_modules:

npm install express cookie-parser body-parser formidable express-session uuid