如何在我的网页上部署我的Nodejs应用程序?

时间:2017-08-06 21:12:27

标签: node.js

制作了一个非常简单的Timestamp Microservice应用程序,其节点我希望能够在我的网站上的网页上运行。我该怎么做呢?它目前在我的本地服务器上运行正常。

我觉得这很简单但是从搜索中只能找到如何部署到Heroku / AWS。

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

//Create an instance of Express for the app and instantiate bodyParser and cors
const app = module.exports = express();
app.use(bodyParser.json());
app.use(cors());

app.get(`/dateValues/:dateVal`, (req,res,next) => {
  //gets date from request
  var dateVal = req.params.dateVal;

  //Options for formatting date in natural state
  var options = { year: 'numeric', month: 'long', day: 'numeric' };

  if(isNaN(dateVal)) {
    var naturalDate = new Date(dateVal);
    naturalDate= naturalDate.toLocaleDateString('en-US', options);
    var unixDate = new Date(dateVal).getTime()/1000-21600;

  } else {
    var unixDate = dateVal;
    var naturalDate = new Date((parseInt(dateVal)+21600)*1000);
    naturalDate= naturalDate.toLocaleDateString('en-US', options);
  }
  res.json({unix: unixDate, natural: naturalDate});
});

app.listen(3000, () => {
  console.log('App is running');
});

2 个答案:

答案 0 :(得分:0)

您是想在自己的服务器上在线推送,它与您在本地的推送方式相同。

安装服务器,安装npm / node,在其上推送项目并运行npm start。这将有效。

如果你想要更好的生产,你可以使用代理网络服务器,如apache或nginx,并使用pm2运行你的nodejs项目

https://www.phusionpassenger.com/library/walkthroughs/deploy/nodejs/ownserver/nginx/oss/trusty/deploy_app.html

答案 1 :(得分:0)

Heroku是node.js应用程序中最简单的部署平台。你也可以免费托管它。查看下面的网址。

https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

相关问题