2016-09-04 4 views
-1

NodeJS値

/*jslint devel: true, vars: true */ 
 

 
var express = require('express'); 
 
var session = require('cookie-session'); 
 
var bodyParser = require('body-parser'); 
 
var urlencodedParser = bodyParser.urlencoded({ 
 
    extended: false 
 
}); 
 

 
var app = express(); 
 

 
app.use(session({ 
 
    name: 'session', 
 
    keys: ['key1', 'key2'], 
 
    secret: 'todotopsecret' 
 
    })) 
 
    .use(function (req, response, next) { 
 
    if (typeof(req.session.todolist) == 'undefined') { 
 
     req.session.todolist = []; 
 
    } else { 
 
    } 
 
    next(); 
 
    }) 
 
    .get('/', function (req, response) { 
 
    "use strict"; 
 
    response.render('todolistMoi.ejs', { 
 
     vecth: req.session.todolist 
 
    }); 
 
    }); 
 

 
app.post('/todo/ajouter/', function (req, res) { 
 
    "use strict"; 
 
}); 
 

 

 
app.get('/todo/supprimer/:id', function (req, res) { 
 
    "use strict"; 
 
    req.session.todolist.splice(req.params.id, 1); 
 
    res.writeHead(302, { 
 
    'Location': 'http://localhost:3615/' 
 
    }); 
 
    res.end(); 
 
}); 
 

 
app.get('/initt', function (reqq, resp) { 
 
    "use strict"; 
 
    reqq.session.todolist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 
 
    resp.writeHead(302, { 
 
    'Location': 'http://localhost:3615/' 
 
    }); 
 
    resp.end(); 
 
}); 
 
    
 
app.listen(3615);

による関数渡しの引数マイtodolistMoi.ejsは次のとおりです。

<!--https://openclassrooms.com/courses/des-applications-ultra-rapides-avec-node-js/tp-la-todo-list --> 
 

 
<!doctype html> 
 
<html lang="fr" ng-app="todoApp"> 
 
    <head> 
 
    <meta charset="UTF-8"> 
 
    </head> 
 
    <body> 
 
    <H2>TODOLISTS</H2> 
 
    <DIV>Liste 
 
     <ul> 
 
     <BR>Longueur= 
 
     <%=vecth.length %> 
 
     <% for(var i=0;i<vecth.length ;i++) { %> 
 
      <li> 
 
      <a href="/todo/supprimer/:<%=i%>">x</a> 
 
      <%=vecth[i]%> 
 
      </li> 
 
     <%}%> 
 
     </ul> 
 
    </DIV> 
 
    <DIV> 
 
     <form method="post" action="/ajouter"> 
 
     <label>Entrer la tache à ajouter</label> 
 
     <input type="text" name="tachee" required> 
 
     <input type="submit" value="Submit"> 
 
     </form> 
 
    </DIV> 
 
    </body> 
 
</html> 
 

そしてpackage.json

{ 
 
    "name": "ma-todolist", 
 
    "version": "0.1.0", 
 
    "dependencies": { 
 
    "express": "~4.11.0", 
 
    "ejs": "~2.1.4", 
 
    "cookie-session": "~1.1.0", 
 
    "body-parser": "~1.10.1" 
 
    }, 
 
    "author": "Mateo21 <[email protected]>", 
 
    "description": "Un gestionnaire de todolist ultra basique" 
 
}
です

私は機能をスプライスする変数req.params.idを与えることを試みます。調査の後、req.params.idは呼び出しの前後で良好な値を取得します。しかし、私の選択肢は、localhost/initt THEN localhost /:それは最初の項目を削除しますが、私の選択したものは削除しませんか?どうして?ライン<a href="/todo/supprimer/:<%=i%>">x</a>

+2

に残念であなたの試験を提案修正をコミットすることができ、あなたは何をあなたが期待する、それが実際に何をするかです説明できますか? – martriay

+0

私は8番目のアイテム(例えば)をクリックします:最初の要素を削除しますが、8番目のアイテムは削除されます。 – moueza

答えて