简体   繁体   中英

How do I deploy my node.js app to a server?

I have created a node server that scrapes through a certain website and then returns a nicely formatted json data. Everything works perfectly fine on my localhost. But now I want to upload it to a server and get a link that returns the json data (just like an api).

How do I do that?

Below is the code outline:

var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app     = express();

app.get('/', gotdata);

function gotdata(req, res){
    url = 'myurlhere';
    request(url, inside_request);

    function inside_request(error, response, html){
        if(!error){
            var $ = cheerio.load(html);
            var title, date, permalink;
            var obj = { title : "", date : "", permalink : ""};
         // ALL MY CODES HERE
        res.send(obj);
        }
    }
}

app.listen('8081');
console.log('Visit http://localhost:8081/');
exports = module.exports = app;

OUTPUT from my localhost:8081/

在此处输入图片说明

You can use an nginx server instead if you have a public IP on the server where you want to deploy your API.
Define a virtual host to your API from nginx configuration.
Please follow the tutorial in the following link for virtual host creation

https://gist.github.com/soheilhy/8b94347ff8336d971ad0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM