-1

/*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);

My todolistMoi.ejs is :

<!--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>
   

And package.json is

{
  "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"
}

I try to give a variable req.params.id to splice function. After investigation, req.params.id get the good value before and after the call. But whatever my choice, localhost/initt THEN localhost/ : it removes first item but not my chosen one? Why?

ZachB
  • 13,051
  • 4
  • 61
  • 89
moueza
  • 69
  • 1
  • 7

1 Answers1

2

on the line <a href="/todo/supprimer/:<%=i%>">x</a> remove the : see : how to get id from url in express, param and query doesnt seem to work

Community
  • 1
  • 1
  • So I've tried without : but still the error. (And as Express API says "app.get('/user/:id" in http://expressjs.com/en/api.html , you must use : – moueza Sep 05 '16 at 07:57