-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (22 loc) · 957 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var express = require("express");
var bodyParser = require("body-parser");
var fs = require ("fs");
var path = require("path");
var estudiantes = require(path.join(__dirname,"datos", "estudiantes.json"));
var servidor = express();
//secuencia de posibles respuestas
servidor.use(express.static(path.join(__dirname, "front")));// MIDDLEWARE
servidor.use(bodyParser.urlencoded({ extended : true }));
servidor.post("/registro", (peticion, respuesta)=>{
console.log(peticion.body);
estudiantes.push(peticion.body);
fs.writeFile(path.join(__dirname, "datos", "estudiantes.json"), JSON.stringify(estudiantes),error =>{
var mensaje = "Estudiante registrado";
if(error){
mensaje = "Ha ocurrido un error";
}
var plantilla = `<h1>${mensaje}</h1><a href="regresar al formulario></a>`;
respuesta.send(plantilla);
});
});
servidor.listen(4000,() => console.log("...ok"));