var http = require('http');
var url = require('url');
var fs = require('fs');
var neededstats = [];
http.createServer(function(req, res) {
if (req.url == '/index.html' || req.url == '/') {
fs.readFile('./index.html', function(err, data) {
res.end(data);
});
} else {
var p = __dirname + '/' + req.params.filepath;
fs.stat(p, function(err, stats) {
if (err) {
throw err;
}
neededstats.push(stats.mtime);
neededstats.push(stats.size);
res.send(neededstats);
});
}
}).listen(8080, '0.0.0.0');
console.log('Server running.');
non ho ancora testato il codice, ma l'altro funziona
Se si desidera ottenere le informazioni percorso dalla richiesta URL
var url_parts = url.parse(req.url);
console.log(url_parts);
console.log(url_parts.pathname);
1.Qualora si stanno ottenendo i parametri dell'URL non sono ancora in grado di leggere il file, basta correggere il percorso del file nel mio esempio. Se si posiziona index.html nella stessa directory di codice del server che avrebbe funzionato ...
2.if avete grande struttura di cartelle che si desidera ospitare utilizzando nodo, allora vi consiglio di utilizzare un quadro di riferimento come expressjs
Se si desidera soluzione grezza al file percorso
var http = require("http");
var url = require("url");
function start() {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
fonte: http://www.nodebeginner.org/
Yep creare una chiamata REST vedere questo articolo http: //erichonorez.wordpress. it/2013/02/10/how-create-a-rest-api-con-node-js-ed-express/ – lastboy
e con il browser, solo per i test rapidi –