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.

7 Replies to “Create Ftp Server using NodeJs”

  1. hello rohit, I have a query related to socket.io and nodejs, when we use nodejs it creates its own server and listens to its own port by default we are using apache server which listens to port 80, how can i integrate node in my existing website which is in PHP. is there any way please help.

    Thanks and hope to hear from you

  2. You can set your nodeJs port whatever you want but make sure that port should empty, Then you can simply integrated your nodejs chat app with your php project.

    Node chat app will not work with your Apache port, you need to create and configure node chat app with another port and integrate with your existing php project..

  3. Hi Rohit
    Thanks for your sharing, but I got problem with running this.
    It can’t found the module file ‘ftpd.js’ in http://ftp.js.
    I thought it maybe should install module of ftpd.js.
    So I try do that “npm install ftpd.js”,but it still doesn’t work.
    How do you do that? Please help me, thanks

  4. IF ftpd module is not calling in your server file please add the path of the file in your app.js file like

    var ftpd = require('./node_modules/ftpd/ftpd.js');

    , If still it does not work for you please send me errors you are getting.

  5. Hi Rohit,

    I had a query that I connect to a ftp client in my terminal I am getting error like this Client connection error: Error: read ECONNRESET and in client side Error: GnuTLS error -15: An unexpected TLS packet was received.
    Error: Could not connect to server.So how can I fix these issues.I hope that I can hear solution from you.Thanks

  6. Hi I tried installing nodeftpd even 32 bit windows machone (win 7). While installing npm I get following errors:
    (does th noteftpd npm work only on linux?)

    npm ERR! Windows_NT 6.1.7600
    npm ERR! argv “C:\\Program Files\\nodejs\\node.exe” “C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js” “install”
    npm ERR! node v6.9.5
    npm ERR! npm v3.10.10
    npm ERR! code EBADPLATFORM

    npm ERR! notsup Unsupported platform for [email protected]: wanted {“os”:”!win32″,”arch”:”any”} (current: {“os”:”win32″,”arch”:”ia32″})
    npm ERR! notsup Valid OS: !win32
    npm ERR! notsup Valid Arch: any
    npm ERR! notsup Actual OS: win32
    npm ERR! notsup Actual Arch: ia32

    npm ERR! Please include the following file with any support request:
    npm ERR! D:\node\npm-debug.log

  7. var server = ftpd.createServer(“127.0.0.1”, “./files/”).listen(2121);
    ^

    TypeError: ftpd.createServer is not a function
    at Object. (Z:\competition\test_project\app.js:11:19)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:508:3

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.