app.js 2.46 KB
var express = require('express');
var bodyParser= require('body-parser');//post할때 필요
var multer = require('multer'); //업로드할 때 필요
var request = require('request');
var http = require('http');

var app= express();

app.locals.pretty = true;
app.use(express.static(__dirname));
app.use('/user',express.static('uploads'));
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(bodyParser.urlencoded({extended:false}));

var imgname;
var storage = multer.diskStorage({ //이 객체의 프로퍼티는 2개갖고 있다.
    destination: function (req, file, cb) {
        cb(null, 'uploads/');//업로드 위치를 저장하는 path함수
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
        cb(null, 'mypic.jpg');
    }
});
var upload = multer({ storage: storage});

// Replace <Subscription Key> with your valid subscription key.
var subscriptionKey = '61650f4a2941462ea2dba74eabefd114';
// You must use the same location in your REST call as you used to get your
// subscription keys. For example, if you got your subscription keys from
// westus, replace "westcentralus" in the URL below with "westus".
var uriBase = 'https://eastasia.api.cognitive.microsoft.com/face/v1.0/detect';
// Request parameters.
var params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,emotion,hair,accessories'
    //'headPose,smile,facialHair,glasses, emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
};

app.get('/home', function(req,res){
    console.log("home");
    res.render('upload');
});

app.post('/uploads', upload.single('userfile'),function(req,res){
    res.render("facedetection");
    imgname = req.file.filename;
});
app.get('/send2api', function (req, res) {

    var options = {
        uri: uriBase,
        qs: params,
        body: '{"url": "http://13.125.213.193:8080/uploads/' + imgname + '"}',
        headers: {
            'Content-Type': 'application/json',
            'Ocp-Apim-Subscription-Key' : subscriptionKey
        }
    };
    request.post(options, (error, response, body) => {
        if (error) {
            console.log('Error: ', error);
            return;
        }
        let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
        console.log('JSON Response\n');
        res.send(jsonResponse);
    });
})

app.listen(8080,function(){
    console.log("connect 8080");
});