Home > front end >  The node operation problem
The node operation problem

Time:03-04

/* *
* Created by Danny on 2015/9/20 15:35.
*/
Var HTTP=the require (" HTTP ");
Var formidable=the require (" formidable ");
Var util=the require (" util ");
Var fs=the require (" fs ");
Var sd=the require (" silly - a datetime ");
Var path=the require (" path ");


//create a server
Var=HTTP server. CreateServer (function (the req, res) {
//if you access the address is this, and request type is post
If (the req. Url=="/dopost" & amp; & The req. Method. ToLowerCase ()=="post") {
//Creates a new incoming form.
Var form=new formidable. IncomingForm ();
//set the file upload store address
Form. UploadDir="./uploads ";
//execute inside the callback function, the form has been received all finished,
Form. The parse (the req, function (err, fields, files) {
//if (err) {
//throw err;
//}
//the console. The log (util. Inspect ({fields: fields, files: files}));

//time, the use of a third-party modules, silly - a datetime
Var TTT=sd. The format (new Date (), "YYYYMMDDHHmmss");
Var ran=parseInt (Math. The random () * 89999 + 10000);
Var extname=path. Extname (files. Tupian. Name);
//execution name
Var oldpath=__dirname + "/" + files. The tupian. Path;
//the new path is made up of three parts: the timestamp, random number, extension
Var newpath __dirname +="/uploads/" + + + extname ran TTT;

//name
Fs. Rename (oldpath newpath, function (err) {
If (err) {
Throw the Error (and fail "renamed");
}
Res. WriteHead (200, {' the content-type: 'text/plain "});
Res. End (" success ");
});
});
} else if (the req. Url=="/") {
//submission form. The HTML page
Fs. ReadFile ("./form. HTML ", function (err, data) {
Res. WriteHead (200, {' the content-type: 'text/HTML});
Res. End (data);
})
} else {
Res. WriteHead (404, {' the content-type: 'text/HTML});
Res. End (" 404 ");
}
});

Server. Listen (80, "127.0.0.1");









In the node, the server is running the also didn't complain, but the web page is blank, and I don't know how to solve
  • Related