gpt4 book ai didi

javascript - 如何在生产环境中运行 Node.js REST API 应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:39 25 4
gpt4 key购买 nike

好吧,我立刻就觉得问这个问题有点愚蠢,但我对 Node.js 应用程序有点陌生,所以请耐心等待。我已经在 node.js 中编写了这个 REST API,它在我的本地计算机上运行得很好。但是,当我使用 webpack 构建它时,我不太确定它应该如何在野外实际运行。在本地,我有一个使用 Express 文件的 server.js,我开始使用 Node 。但是我将如何运行构建版本呢?我确信答案是显而易见的,只是我没有看到。

我的目标是能够在我的共享托管解决方案的子域上运行它,因此我会有类似 https://myapi.mydomain.com/getAListOfSomething/ 的示例端点

所以底线 - 我想使用 Webpack 来构建我的应用程序,然后将其部署到某个地方并像普通 API 一样使用它。我只是不知道该怎么做。对于像 React 应用程序这样的东西,这对我来说是完全显而易见的(一个带有脚本标记的 index.html 文件到我的应用程序,非常简单),但是对于这样的东西我迷失了。

这是一些代码...

SERVER.JS

const routes = require('./routes/appRoutes.js');
const express = require('express');
const cors = require('cors');

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const envPath = process.env.NODE_ENV !== 'production' ? `.env.${process.env.NODE_ENV}` : '.env';
const config = require('dotenv').config({path: envPath});

bodyParser = require('body-parser');
app = express();
port = process.env.PORT || 3001;
app.use(cors());
app.listen(port);
console.log(process.env.APP_NAME + ' started on port ' + port +' (yay!)');

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

routes(app); //register the route

* WEBPACK.CONFIG.JS *

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

const envPath = process.env.NODE_ENV !== 'production' ? `.env.${process.env.NODE_ENV}` : '.env';
const config = require('dotenv').config({path: envPath});


module.exports = (env) => {
const isProduction = env==='production';

return {
entry: './routes/appRoutes.js',
output: {
path: path.join(__dirname,'public','dist'),
filename: 'bundle.js'
},
target: 'node',
node: {
// Need this when working with express, otherwise the build fails
__dirname: false, // if you don't put this is, __dirname
__filename: false, // and __filename return blank or /
},
externals: [nodeExternals()],
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
}]
},
plugins: [
new webpack.DefinePlugin({
'process.env.APP_NAME': JSON.stringify(process.env.APP_NAME),
'process.env.DB_HOST': JSON.stringify(process.env.DB_HOST),
'process.env.DB_USERNAME': JSON.stringify(process.env.DB_USERNAME),
'process.env.DB_PASSWORD': JSON.stringify(process.env.DB_PASSWORD),
'process.env.DB_PASSWORD': JSON.stringify(process.env.DB_PASSWORD),
'process.env.PORT': JSON.stringify(process.env.PORT)
})
],
devtool: isProduction ? 'source-map' : 'inline-source-map',
devServer: {
contentBase: path.join(__dirname,'public'),
port: 3300,
historyApiFallback: true,
publicPath: '/dist/'
}
}
};

PACKAGE.JSON

{
"name": "spinder-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build:dev": "webpack -p --env development",
"build:prod": "webpack -p --env production",
"dev-webpack": "webpack-dev-server --env development",
"dev-server": "node app.js --env development"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-watch": "^7.0.0",
"bcrypt": "^3.0.4",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"db-migrate-mysql": "^1.1.10",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"extract-text-webpack-plugin": "^3.0.2",
"multer": "^1.4.2",
"mysql": "^2.16.0",
"type-of-is": "^3.5.1"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/preset-env": "^7.6.2",
"babel-loader": "^8.0.6",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"nodemon": "^1.18.10",
"webpack": "^4.41.0",
"webpack-cli": "^3.3.9",
"webpack-dev-middleware": "^3.7.2",
"webpack-dev-server": "^3.8.2",
"webpack-hot-middleware": "^2.25.0",
"webpack-node-externals": "^1.7.2"
}
}

.ENV[.DEVELOPMENT] 示例(注意:当然,我有开发版本和生产版本......)

APP_NAME=spinder_api
DB_HOST=localhost
DB_USERNAME=db_username
DB_PASSWORD=my_strong_passwrod
DB_DATABASE=my_app_database
PORT=3300

最佳答案

在您要构建到 public/dist/ 的 webpack 配置中,应该有一个 bundle.js 可以运行。然后只需node bundle.js

对于 Node 项目来说,这似乎是一个奇怪的位置,但很容易更改。

关于javascript - 如何在生产环境中运行 Node.js REST API 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58288145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com