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



制作了一个非常简单的时间戳微服务应用程序,我希望能够在我网站上的网页上运行。我将如何做到这一点?它目前在我的本地服务器上工作正常。

我觉得这很简单,但从搜索中只能找到如何部署到 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');
});

你想在自己的服务器上在线推送它,它将与你在本地所做的相同。

安装你的服务器,安装 npm/node,在上面推送你的项目并运行 npm start。这将起作用。

如果你想在生产方面做得更好一点,你可以使用代理Web服务器,如apache或nginx,并使用pm2运行你的nodejs项目

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

Heroku 是最简单的部署平台,当涉及到 node.js 应用程序时。您也可以免费托管它。查看下面的网址。

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

最新更新