Using nodejs application in remote server

Viewed 66

I am trying to upload files using nodejs. Here's the code I tried locally.

var http = require('http');
var express = require("express");
var fs = require('fs');
var formidable = require('formidable');
var upload_html = fs.readFileSync("upload_file.html");
var upload_path = "./uploads/";
http.createServer(function (req, res) {
    if (req.url == '/uploadform') {
      res.writeHead(200);
      res.write(upload_html);
      return res.end();
    } else if (req.url == '/fileupload') {
        var form = new formidable.IncomingForm();
        form.parse(req, function (err, fields, files) {
            var oldpath = files.filetoupload.path;
            var newpath = upload_path + files.filetoupload.name;
            fs.rename(oldpath, newpath, function (err) {
                if (err) throw err;
                res.write('File uploaded and moved!');
                res.end();
            });
        });
    } 
}).listen(8086);
<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
<style>
    body{text-align:center;}
    form{display:block;border:1px solid black;padding:20px;}
</style>
</head>
<body>
    <h1>Upload files to Node.js Server</h1>
 
    <form action="fileupload" method="post" enctype="multipart/form-data">
        <input type="file" name="filetoupload">
        <input type="submit" value="Upload">
    </form>
</body>
</html>

Now, I want to push this to a remote server. I already have an IP to the server. Can someone please explain what are the necessary steps to be followed and what are changes to be made to the code. I tried copying the files to server and when I try to run node index.js it's not working

0 Answers
Related