Node.js ‹¬™”ê³¼ •

  • View
    388

  • Download
    6

Embed Size (px)

Text of Node.js ‹¬™”ê³¼ •

  • Node.js

    - 3

    /, , DB

    syhong0714@gmail.com

  • 3 Node.js

    express

    Expressgenerator

    express-eejs -clessmoca_server

  • 3 Node.js

    Routes API

    var routes=require('./routes/index');var users=require('./routes/users');var users2=require('./routes/users2');var books=require('./routes/books');var proposals=require('./routes/proposals');var ejsbooks =require('./routes/ejsbooks');

    app.use('/', routes);app.use('/users',users2);app.use('/api/1/users',users);app.use('/api/1/books', books);app.use('/api/1/proposals', proposals);

    ->

  • 3 Node.js

    API

    app.use('/', routes);app.use('/users',users2);app.use('/api/1/users',users);app.use('/api/1/books', books);app.use('/api/1/proposals', proposals);

    GET,POST,PUT,DELETE

  • 3 Node.js

    ./routes/users.jsusers api

    var express=require('express');var router=express.Router();varmysql =require('mysql');

    var client=mysql.createConnection({host:'localhost',user:'root',database:'test'});

  • 3 Node.js

    /

    users api

    /*/(select)- user_type:M(),P()+++!!!*///moca.com/api/1/users?user_type=Mrouter.get('/',function(req,res,next){var user_type =req.query.user_type;if(user_type =='P'||'M'){client.query('SELECT*FROMuserWHEREuser_type =?',user_type, function

    (err,rows,fields) {if(err){console.log(err);}varusers=[];rows.forEach(function(row){users.push({id :row.id,username:row.username,address :row.address,price :row.price,user_type :row.user_type});});res.json({users: users});});

    }});

    router/get(/,callback)

  • 3 Node.js

    Users()

    router/post(/,callback)

    /*users(,insert)*///moca.com/api/1/usersrouter.post('/', function(req,res,next){var data={'username':req.body.username,'address':req.body.address,'price':req.body.price

    }

    client.query('INSERT*intouserSET?',data,function(err,rows,fields) {

    if(err){conosole.log(err);}res.status(200);

    });});

  • 3 Node.js

    Users()

    router/put(/:user_id,callback)

    /*/ & -. user_id *///moca.com/api/1/users/2?user_type=Mrouter.put('/:user_id', function(req,res,next){var user_id =req.params.user_id;var user_type =req.query.user_type;

    client.query('UPDATEuserSETuser_type =?WHEREid=?',[user_type,user_id],function(err,rows,fields) {

    if(err){console.log(err);}

    });});

  • 3 Node.js

    Users()

    Router/get(/,callback)

    /* - admin *///moca.com/api/1/usersrouter.get('/', function(req,res,next){var user_type =req.query.user_type;

    client.query('SELECT*FROMuserWHEREuser_type =?',user_type, function(err, rows,fields) {

    if(err){console.log(err);}

    varusers=[];rows.forEach(function(row){users.push({id:row.id,username:row.username,address:row.address,price:row.price,user_type :row.user_type});});res.json({users:users});

    });});

  • 3 Node.js

    books( )

    Router/get(/,callback)

    /*books(select)*/router.get('/', function(req,res,next){client.query('SELECTb.id ASbooks_id, b.username ASbooks_username, i.img_url ASimg_url_img_url FROMbooksbJOINimg_url i ONb.img_url1=i.id',function(err,rows,fields) {

    if(err){console.log(err);}

    varbooks=[];rows.forEach(function(row){books.push({id:row.books_id,username:row.books_username,img_url :row.img_url_img_url});});res.json({books:books});

    });});

  • 3 Node.js

    ?

    ,,

    connect.sid

    ()

  • 3 Node.js

    view(get)app.get('/auth/login', function(req,res){var output=`Login

  • 3 Node.js(post)

    app.post('/auth/login', function(req,res){var user={username:'kevin',password:'1234',displayName:'kevin hong'

    };

    var uname =req.body.username;var pwd =req.body.password;

    if(uname ===user.username &&pwd ===user.password) {res.redirect('/welcome');

    }else{res.send('Whoareyou?login');

    }});

    var express=require('express');var session=require('express-session');var bodyParser =require('body-parser');

  • 3 Node.js

    var express=require('express');var session=require('express-session');var bodyParser =require('body-parser');varMySQLStore =require('express-mysql-session')(session);

    app.use(session({secret:'djfivjdFDjfh1@1@$ei%#213#',resave:false,saveUninitialized:true,store:newMySQLStore({host:'localhost',port:3306,database:'test'

    })}));

  • 3 Node.js +

    app.post('/auth/login', function(req,res){var user={username:'kevin',password:'1234',displayName:'kevin hong'

    };

    var uname =req.body.username;var pwd =req.body.password;

    if(uname ===user.username &&pwd ===user.password) {req.session.displayName =user.displayName;req.session.save(function(){res.redirect('/welcome');});

    }else{res.send('Whoareyou?login');

    }});

    app.get('/welcome', function(req,res){if(req.session.displayName) {res.send(`Hello,${req.session.displayName}logout`);

    }else{res.send(`WelcomeLogin`);

    }});

  • 3 Node.jsDB

  • 3 Node.js

    md5

    SHA256pbkdf2-passwordnpm

    salt

    if(uname ===user.username &&pwd ===user.password)

  • 3 Node.js

    passportjsredis

    Strategy:local,facebook

  • 3 Node.js

    Ejs json api

  • Node.js

    - 3

    /

    syhong0714@gmail.com

  • 3 Node.js

    pbkdf2-passwordnpm

    var bkfd2Password=require("pbkdf2-password");var hasher=bkfd2Password();

    crypto

    hasher({password:pwd, salt:user.salt},function(err,pass,salt,hash){if(hash===user.password) {done(null,user);

    }else{done(null, false);

    }});

  • 3 Node.js

    pbkdf2-passwordnpm crypto

  • 3 Node.js

    passport.js

    Strategy

    LocalStrategy FacebookStr

    GoogleStr

    TwitterStr

    Kakao,Naver,

  • Passport- local

    var passport=require('passport');var LocalStrategy =require('passport-local').Strategy;

    app.post('/auth/login',passport.authenticate('local',{successRedirect:'/welcome',failureRedirect:'/auth/login',failureFlash:false}

    ));

    passport.use(newLocalStrategy(function(username,password,done) {varuname =username;varpwd =password;

    for(var i =0;i