如何部署 nodejs 并快速部署到 AWS EC2 实例



我有一个需要部署的 MEAN 应用程序,我选择将其部署在 AWS Linux AMI 上,但在部署它时遇到了一些问题。

根据我到目前为止阅读的教程,每个nodejs和express都包含html页面。

如何在没有 html 页面的情况下部署它? 就像他们去域,他们只看到一个空白页?

我知道我可以清空一个html文件,但我还有其他解决方案吗?

这是我的服务器.js

const http = require("http");
const debug = require("debug")("sales-and-inventory");
const app = require("./app");
// normalizePort() makes sure that the port is a valid number data type
const normalizePort = val => {
var port = parseInt(val, 10);
if (isNaN(port)) {
// isNot-a-Number
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
};
// checks the type of error occured
const onError = error => {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
};
// just logging
const onListening = () => {
const addr = server.address();
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
debug("Listening on " + bind);
};
// set a config for the express environment & config for the port
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
const server = http.createServer(app);
// registered on this listeners on the function above
server.on("error", onError);
server.on("listening", onListening);
// start server
server.listen(port);

这是应用程序.js

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const authRoutes = require("../routes/auth.route");
const userRoutes = require("../routes/user.route");
const customerRoutes = require("../routes/customer.route");
const vendorRoutes = require("../routes/vendor.route");
const salesRoutes = require("../routes/sales-order.route");
const purchaseRoutes = require("../routes/purchase-order.route");
const inventoryRoutes = require("../routes/inventory.route");
const transferRoutes = require("../routes/transfer.route");
const paymentRoutes = require("../routes/payment.route");

const app = express();
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
mongoose.connect(process.env.ATLAS_CONNECTION_STRING, { useNewUrlParser: true })
.then(() => {
console.log("Connected to Atlas");
})
.catch(err => {
if (err) {
console.log(err);
}
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// CORS
app.use((req, res, next) => {
// allows any domain to access our resources
res.setHeader("Access-Control-Allow-Origin", "*");
// allows domain with a certain set of headers
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
// allows http type of requests
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
res.header('Content-Security-Policy', 'img-src "self"');
next();
});


app.use("/api/auth", authRoutes);
app.use("/api/user", userRoutes);
app.use("/api/customer", customerRoutes);
app.use("/api/vendor", vendorRoutes);
app.use("/api/sales", salesRoutes);
app.use("/api/purchase", purchaseRoutes);
app.use("/api/inventory", inventoryRoutes);
app.use("/api/transfer", transferRoutes);
app.use("/api/payment", paymentRoutes);
module.exports = app;

启动 EC2 实例后,如果您使用的是 Windows go for PUTTY,则可以继续在计算机上设置 SSH。

按照本教程在登录到实例后部署 NodeJS 应用程序。

https://www.youtube.com/watch?v=tasoWTGM1hA

在部署之前 确保将安全组添加到 EC2 实例并添加 HTTP、具有任何 IP 地址的 HTTPS 角色,我的意思是 0.0.0.0 和任何端口:0

相关内容

  • 没有找到相关文章

最新更新