-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservidor.js
83 lines (78 loc) · 2.28 KB
/
servidor.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//visitar la documentacion nodejs.org
// Ejemplo 1
var http=require("http");
console.log("Inicio");
var host="127.0.0.1";
var port=1337;
var server=http.createServer(function(request,response){
console.log("Petición recibida: "+request.url);
response.writeHead(200,{"Content-type":"text/plain"});
response.end("Hola mundo");
});
server.listen(port,host,function(){
console.log("Escuchando "+host+":"+port)
});
// Ejemplo 2
//Para este ejemplo hay que crear un directorio public (/node/public)
//e introducir en ese directorio un archivo html
/*
var http=require("http");
var fs=require("fs");
console.log("Inicio");
var host="127.0.0.1";
var port=1337;
var server=http.createServer(function(request,response){
console.log("Petición recibida:"+request.url);
fs.readFile("./public"+request.url,function(error,data){
if(error){
response.writeHead(404,{"Content-type":"text/plain"});
response.end("Lo siento, página no encontrada");
}
else{
response.writeHead(200,{"Content-type":"text/html"});
response.end(data);
}
})
//response.writeHead(200,{"Content-type":"text/plain"});
//response.end("Hola mundo");
});
server.listen(port,host,function(){
console.log("Escuchando "+host+":"+port)
});
*/
//Ejemplo 3
//Leemos la configuración (host y puerto) desde el archivo json (config.json)
//prueba modificar la configuracion
/*
var http=require("http");
var fs=require("fs");
console.log("Inicio");
var config=JSON.parse(fs.readFileSync("config.json"));
var host=config.host;//"127.0.0.1";
var port=config.port;//1337;
var server=http.createServer(function(request,response){
console.log("Petición recibida:"+request.url);
fs.readFile("./public"+request.url,function(error,data){
if(error){
response.writeHead(404,{"Content-type":"text/plain"});
response.end("Lo siento, página no encontrada");
}
else{
response.writeHead(200,{"Content-type":"text/html"});
response.end(data);
}
})
//response.writeHead(200,{"Content-type":"text/plain"});
//response.end("Hola mundo");
});
server.listen(port,host,function(){
console.log("Escuchando "+host+":"+port)
});
fs.watchFile("config.json",function(){
config=JSON.parse(fs.readFileSync("config.json"));
server.close();
server.listen(config.port,config.host,function(){
console.log("Escuchando "+config.host+":"+config.port)
});
})
*/