Create Ftp Server using NodeJs

How to create ftp server in node.js

Today I am going to create ftp server in my localhost using one of nodejs module(nodeftpd),
node-ftp


First Install nodejs on your system.
http://howtonode.org/how-to-install-nodejs

Install npm
http://blog.npmjs.org/post/85484771375/how-to-install-npm

Create package.json file in your app root directory and paste below code.

package.json

{
"name": "NodeFtpServer",
"description": "Create ftp srver using nodeftpd module",
"version": "0.0.1",
"dependencies": {
"nodeftpd": "*"
},
"engines": {
"node": "0.10.x"
}
}

Now goto your app directory where you have placed package.json file and run this command from terminal

$ npm install

this command will read all the dependencies from package.json and install them in your app directory




Create directory name files under your main app folder. and create another directory where you want to upload your files from ftp in may case it is rohit
it looks like nodeftpserver/files/rohit

Now create file with name ftp.js
Default ftp port is 21 but you can set any port you want , I am using port 2121
Copy and Paste below code in ftp.js file

var ftpd = require('ftpd.js');
var server = ftpd.createServer("127.0.0.1", "./files/").listen(2121);
server.on("client:connected", function(socket) {
var username = null;
console.log("client connected: " + socket.remoteAddress);
socket.on("command:user", function(user, success, failure) {
if (user) {
username = user;
success();
} else failure();
});
socket.on("command:pass", function(pass, success, failure) {
if (pass) success(username);
else failure();
});
});
server.debugging = 4;
server.listen(5555);

Your directory structure lookes like

nodeftpserver (main dir)
--node_modules (dir)
--package.json (file)
--ftp.js (file)
--files (dir)
----rohit (dir)

If you are using port 21 you may have to include sudo before this command to run ftp server.

$ sudo node ftp.js
OR
$ node ftp.js
Now open any ftp client and ftp credentials will be.
Hostname: 127.0.0.1
Username: rohit (your username willbe same as folder name which you have created under files directory )
Password: Anything but should not blank.
port: 2121 (you can choose any empty port just change in ftp.js file)

That’s It
Thanks..!! 🙂

If you like this post please don’t forget to subscribe My Public Notebook for more useful stuff.