this is my form how can i connect with node js.while submitting this form
<form id="fileupload" action="/file-upload" method="POST" enctype="multipart/form-data">
<input id="fileToBeUploaded" type="file" name="thumbnail">
<input type="submit" value="upload">
</form>
server side
var http = require("http");
var url = require("url");
http.createServer(function(req, res) {
switch (url.parse(req.url).pathname)
{
case '/': console.log('server side methodcalled');
break;
case '/file-upload': console.log('server side method called');
break;
default:
console.log('server side method called');
break;
}
});
In your nodejs server program you can check for request method and action.
if((request.method == 'POST') && (request.url == '/file-upload') {
.......
}
Updated -
var http = require("http");
var url = require("url");
var fs = require("fs");
http.createServer(function(req, res) {
switch (url.parse(req.url).pathname)
{
case '/':
console.log('server side methodcalled');
fs.readFile(__dirname + '/index.html',
function (err, data) {
res.writeHead(200);
res.end(data);
});
break;
case '/file-upload':
console.log('server side method called');
break;
default:
console.log('server side method called');
break;
}
}).listen(3000);
Now you can connect to localhost:3000
In order to implement file uploading, you'll need to read the body of the request and parse out the various multiple parts. This can be tricky, so I'd strongly suggest you use an existing module like formidable, or something that invokes it behind the scenes like connect.bodyParser() or express.bodyParser().