NodeJS函数按值传递参数

时间:2016-09-04 07:26:21

标签: node.js function splice

/*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 <mateo21@email.com>",
  "description": "Un gestionnaire de todolist ultra basique"
}

我尝试将变量req.params.id赋予splice函数。经过调查,req.params.id在通话前后获得了良好的价值。但无论我选择什么,localhost / initt那么localhost /:它会删除第一个项目而不是我选择的项目?为什么呢?

2 个答案:

答案 0 :(得分:2)

<a href="/todo/supprimer/:<%=i%>">x</a>行上

删除:,请参阅:how to get id from url in express, param and query doesnt seem to work

答案 1 :(得分:0)

您可以直接在https://github.com/moueza/nodeJS-TP-todolistBugOverflow

上提交经过测试的建议更正
相关问题