如何将express.js服务器部署到Netlify [英] How to deploy express.js server to Netlify

查看:187
本文介绍了如何将express.js服务器部署到Netlify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将Vue.js,Node,Express,MongoDB(MEVN)堆栈应用程序部署到Netlify.我已成功将应用程序的前端部署到Netlify,现在正根据以下serverless-http示例尝试部署Express服务器:

I am attempting to deploy a Vue.js, Node, Express, MongoDB (MEVN) stack application to Netlify. I successfully deployed the front end of the application to Netlify, and am now attempting to deploy the express server, based on the following serverless-http example: https://github.com/neverendingqs/netlify-express/blob/master/express/server.js

我将服务器配置为包含serverless-http软件包:

I configured my server to include the serverless-http package:

server.js

server.js

const express = require('express');
const app = express();
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const config = require('./DB.js');
const postRoute = require('./routes');

mongoose.connect(config.DB, { useNewUrlParser: true, useUnifiedTopology: true }).then(
  () => { console.log('Database is connected') },
  err => { console.log('Can not connect to the database'+ err)}
);

app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.use('/messages', postRoute);

app.use('/.netlify/functions/server', router);  // path must route to lambda
app.use('/', (req, res) => res.sendFile(path.join(__dirname, '../public/index.html')));


module.exports = app;
module.exports.handler = serverless(app);

routes.js

routes.js

const express = require('express');
const postRoutes = express.Router();

// Require Post model in our routes module
let Post = require('./post.model');

// Defined store route
postRoutes.route('/add').post(function (req, res) {
  let post = new Post(req.body);
  post.save()
    .then(() => {
      res.status(200).json({'business': 'business in added successfully'});
    })
    .catch(() => {
      res.status(400).send("unable to save to database");
    });
});

// Defined get data(index or listing) route
postRoutes.route('/').get(function (req, res) {
    Post.find(function(err, posts){
    if(err){
      res.json(err);
    }
    else {
      res.json(posts);
    }
  });
});

module.exports = postRoutes;

然后,我将我的应用程序重新部署到Netlify,但是服务器似乎未在Netlify中运行.该服务器位于我的vue.js应用程序项目根目录中的文件夹中.我应该在Netlify中将服务器作为单独的站点运行吗?如果没有,我应该怎么做才能使服务器在Netlify中部署后运行?

I then re-deployed my application to Netlify, but the server does not seem to run in Netlify. This server is in a folder in project root of my vue.js app. Should I instead run the server as a separate site in Netlify? If not, what should I do in order to get the server to run when deployed in Netlify?

推荐答案

已经有一段时间了,但是可以了.

It's been a while, but here goes.

Netlify托管用于Jamstack,正如他们所说,即仅静态文件,服务器上无处理.这个想法是利用其他机制来动态获取数据,例如在其他地方托管的API,您可以直接从浏览器中查询,也可以在构建网站时查询.

Netlify hosting is for the Jamstack, as they say, i.e. only static files, no processing on the server. The idea is to make use of other mechanisms to get your data dynamically, such as APIs hosted elsewhere, which you query straight from the browser, or when you build your site.

实际上,您实际上很有可能必须将express.js应用程序部署为Netlify函数.检查 Netlify的博客文章,介绍如何在其功能上运行快速应用程序.

Most likely you actually had to deploy your express.js app as a Netlify Function, instead. Check Netlify's blog post on running express apps on their functions.

这篇关于如何将express.js服务器部署到Netlify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆