使用railway部署Node项目及遇到的问题

这篇具有很好参考价值的文章主要介绍了使用railway部署Node项目及遇到的问题。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

大家好, 今天愚人节, 祝大家节日快乐~

同时向大家推荐一个非常nice的网站, railway, 它能够免费部署项目, 并且免费的账号.

一个月有20天使用权限, 如果想要更长的时间, 就需要续费, 但是一个月20天免费, 还是非常的划算的.

大家都可以去试试, 并且还有一个好处, 它的域名是国外的, 也就是说你部署请求chatGPT的后端服务, 那速度也是非常的丝滑.

是不是听到这里的你, 已经有跃跃欲试的喜悦了, 继续看下面内容, 能够帮你快速开发~~

docker部署

网址:

https://railway.app/
账号:GitHub账号, 可以直接跟自己的GitHub仓库直接关联, 将GitHub中的项目直接部署

官方文档

https://docs.railway.app/deploy/deploy-on-railway-button

自己选择要部署的项目种类

里面有各种各样项目的demo,如果不知道如何配置,可以查找一下
https://railway.app/templates

Deploy HTTP NodeJS
https://railway.app/new/template/ZweBXA

我成功部署的代码仓库
https://github.com/bluelightsky/vue3-h5-http

部署后端服务常遇到的问题

1.端口随机

这个PORT不要在.env文件中显示出来

const app = express()
const port = process.env.PORT || 3333;
app.use(express.json());

指定一个端口,当这3个端口被占用时,没有任何反应, 只是接口503, 一直都请求不成功

使用railway部署Node项目及遇到的问题

2.使用yarn下载,

如果部署成功之后, 在控制台一直报错, can’t find yarn command

记得本地使用yarn生成yarn.lock文件

3.docker部署

方法1: Procfile

需要一个Procfile文件, 这个文件指定在项目编译成功之后, 项目的启动命令

使用railway部署Node项目及遇到的问题

我的项目中就只有这个命令
这个命令是在package.json文件中的script中设置, 可以设置成你想要启动的任何命令

web: yarn start

方法2: Dockerfile

创建Dockerfile文件, 也是一样的目的, 项目编译成功之后, 启动的命令

# 指定 node 版本号,满足宿主环境
FROM node:16-alpine as builder

# 指定工作目录,将代码添加至此
WORKDIR /code
ADD . /code

# 如何将项目跑起来
ADD package.json package-lock.json /code

RUN yarn install
RUN yarn run build
RUN yarn start

4.ts编译问题

使用CommonJS编译

就是编译ts之后, js文件是使用require引入的

{
  "compilerOptions": {
    "outDir": "dist",
    "incremental": true,
    "target": "es6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

使用ESModule编译

在设置tsconfig.json之前, 需要把package.json中的**“type”: “module”**, 如果没有type属性就手动加上, 因为默认这个type是CommonJS模块

报错如下:
(node:16988) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.

就是编译ts文件之后, js文件使用import引入的
chatgpt这个npm包就必须要使用import导入, 所以如果编译之后文件是CommonJS, 就会报错

使用railway部署Node项目及遇到的问题

{
  "extends": "@tsconfig/node18/tsconfig.json",
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    /* Projects */
    // "incremental": true,                              /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
    // "composite": true,                                /* Enable constraints that allow a TypeScript project to be used with project references. */
    // "tsBuildInfoFile": "./.tsbuildinfo",              /* Specify the path to .tsbuildinfo incremental compilation file. */
    // "disableSourceOfProjectReferenceRedirect": true,  /* Disable preferring source files instead of declaration files when referencing composite projects. */
    // "disableSolutionSearching": true,                 /* Opt a project out of multi-project reference checking when editing. */
    // "disableReferencedProjectLoad": true,             /* Reduce the number of projects loaded automatically by TypeScript. */

    /* Language and Environment */
    "target": "ESNext",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    // "lib": [],                                        /* Specify a set of bundled library declaration files that describe the target runtime environment. */
    // "jsx": "preserve",                                /* Specify what JSX code is generated. */
    // "experimentalDecorators": true,                   /* Enable experimental support for TC39 stage 2 draft decorators. */
    // "emitDecoratorMetadata": true,                    /* Emit design-type metadata for decorated declarations in source files. */
    // "jsxFactory": "",                                 /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
    // "jsxFragmentFactory": "",                         /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
    // "jsxImportSource": "",                            /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
    // "reactNamespace": "",                             /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
    // "noLib": true,                                    /* Disable including any library files, including the default lib.d.ts. */
    // "useDefineForClassFields": true,                  /* Emit ECMAScript-standard-compliant class fields. */
    // "moduleDetection": "auto",                        /* Control what method is used to detect module-format JS files. */

    /* Modules */
    "module": "ESNext",                                  /* Specify what module code is generated. */
    // "rootDir": "./",                                  /* Specify the root folder within your source files. */
    "moduleResolution": "Node",                          /* Specify how TypeScript looks up a file from a given module specifier. */
    // "baseUrl": "./",                                  /* Specify the base directory to resolve non-relative module names. */
    // "paths": {},                                      /* Specify a set of entries that re-map imports to additional lookup locations. */
    // "rootDirs": [],                                   /* Allow multiple folders to be treated as one when resolving modules. */
    // "typeRoots": [],                                  /* Specify multiple folders that act like './node_modules/@types'. */
    // "types": [],                                      /* Specify type package names to be included without being referenced in a source file. */
    // "allowUmdGlobalAccess": true,                     /* Allow accessing UMD globals from modules. */
    // "moduleSuffixes": [],                             /* List of file name suffixes to search when resolving a module. */
    "resolveJsonModule": true,                           /* Enable importing .json files. */
    // "noResolve": true,                                /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

    /* JavaScript Support */
    // "allowJs": true,                                  /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
    // "checkJs": true,                                  /* Enable error reporting in type-checked JavaScript files. */
    // "maxNodeModuleJsDepth": 1,                        /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */

    /* Emit */
    // "declaration": true,                              /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
    // "declarationMap": true,                           /* Create sourcemaps for d.ts files. */
    // "emitDeclarationOnly": true,                      /* Only output d.ts files and not JavaScript files. */
    "sourceMap": true,                                   /* Create source map files for emitted JavaScript files. */
    // "outFile": "./",                                  /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
    "outDir": "dist",                                    /* Specify an output folder for all emitted files. */
    // "removeComments": true,                           /* Disable emitting comments. */
    // "noEmit": true,                                   /* Disable emitting files from a compilation. */
    // "importHelpers": true,                            /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
    // "importsNotUsedAsValues": "remove",               /* Specify emit/checking behavior for imports that are only used for types. */
    // "downlevelIteration": true,                       /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
    // "sourceRoot": "",                                 /* Specify the root path for debuggers to find the reference source code. */
    // "mapRoot": "",                                    /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,                          /* Include sourcemap files inside the emitted JavaScript. */
    // "inlineSources": true,                            /* Include source code in the sourcemaps inside the emitted JavaScript. */
    // "emitBOM": true,                                  /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
    // "newLine": "crlf",                                /* Set the newline character for emitting files. */
    // "stripInternal": true,                            /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
    // "noEmitHelpers": true,                            /* Disable generating custom helper functions like '__extends' in compiled output. */
    // "noEmitOnError": true,                            /* Disable emitting files if any type checking errors are reported. */
    // "preserveConstEnums": true,                       /* Disable erasing 'const enum' declarations in generated code. */
    // "declarationDir": "./",                           /* Specify the output directory for generated declaration files. */
    // "preserveValueImports": true,                     /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */

    /* Interop Constraints */
    // "isolatedModules": true,                          /* Ensure that each file can be safely transpiled without relying on other imports. */
    "allowSyntheticDefaultImports": true,                /* Allow 'import x from y' when a module doesn't have a default export. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    // "preserveSymlinks": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "noImplicitAny": true,                               /* Enable error reporting for expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,                         /* When type checking, take into account 'null' and 'undefined'. */
    // "strictFunctionTypes": true,                      /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
    // "strictBindCallApply": true,                      /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
    // "strictPropertyInitialization": true,             /* Check for class properties that are declared but not set in the constructor. */
    // "noImplicitThis": true,                           /* Enable error reporting when 'this' is given the type 'any'. */
    // "useUnknownInCatchVariables": true,               /* Default catch clause variables as 'unknown' instead of 'any'. */
    // "alwaysStrict": true,                             /* Ensure 'use strict' is always emitted. */
    // "noUnusedLocals": true,                           /* Enable error reporting when local variables aren't read. */
    // "noUnusedParameters": true,                       /* Raise an error when a function parameter isn't read. */
    // "exactOptionalPropertyTypes": true,               /* Interpret optional property types as written, rather than adding 'undefined'. */
    // "noImplicitReturns": true,                        /* Enable error reporting for codepaths that do not explicitly return in a function. */
    // "noFallthroughCasesInSwitch": true,               /* Enable error reporting for fallthrough cases in switch statements. */
    // "noUncheckedIndexedAccess": true,                 /* Add 'undefined' to a type when accessed using an index. */
    // "noImplicitOverride": true,                       /* Ensure overriding members in derived classes are marked with an override modifier. */
    // "noPropertyAccessFromIndexSignature": true,       /* Enforces using indexed accessors for keys declared using an indexed type. */
    // "allowUnusedLabels": true,                        /* Disable error reporting for unused labels. */
    // "allowUnreachableCode": true,                     /* Disable error reporting for unreachable code. */

    /* Completeness */
    // "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

既然是使用ts文件, 所以不能直接需要使用tsx编译, 所以package.json文件中需要有ts编译命令, 以及启动命令

"scripts": {
    "dev": "nodemon src/index.ts",
    "build": "tsc",
    "start": "node dist/index.js"
  },

5.设置部署环境中的node版本

文档: https://nixpacks.com/docs/providers/node

railway默认是v14版本, 如果需要修改根据官方文档中描述的, 指定环境变量中NIXPACKS_NODE_VERSION

使用railway部署Node项目及遇到的问题
后续继续分享部署前端项目的方式, 敬请期待~~~文章来源地址https://www.toymoban.com/news/detail-471600.html

到了这里,关于使用railway部署Node项目及遇到的问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 今天给大家带来Python炫酷爱心代码

    前言: 这个是小编之前朋友一直要小编去做的,不过之前技术不够所以一直拖欠今天也完成之前的约定吧! 至于他是谁,我就不多说了直接上代码 如果有需要的话,可以联系小编噢!

    2024年02月05日
    浏览(38)
  • 今天给大家介绍一下华为智选手机与华为手机的区别

    华为智选手机是由华为品牌方与其他公司合作推出的手机产品,虽然其机身上没有“华为”标识,但是其品质和技术水平都是由华为来保证的。这些手机在制造、设计和使用方面都采用了华为的相关技术和标准,因此可以享受到和华为旗舰手机相同的优质使用体验。    目前

    2024年02月09日
    浏览(37)
  • 今天跟大家分享好用的智能ai绘画免费软件有哪些

    在教学的时候配合进行ai绘画操作来讲解日常的知识,可以帮助学生更好的理解,比如在讲解化学反应的时候,我们可以通过文字描述反应的化学式和反应过程,但是这可能会让学生感到无趣和枯燥,知识没办法真正的进入大脑。而如果借助ai绘画的软件,我们可以将反应的过

    2024年02月12日
    浏览(36)
  • 今天跟大家好好介绍一下接口工具(jmeter、postman、swagger等)

    一、接口都有哪些类型? 接口一般分为两种:1.程序内部的接口 2.系统对外的接口 系统对外的接口:比如你要从别的网站或服务器上获取资源或信息,别人肯定不会把 数据库共享给你,他只能给你提供一个他们写好的方法来获取数据,你引用他提供的接口就能使用他写好的

    2024年02月05日
    浏览(42)
  • 今天跟大家推荐几款实用的ai写作生成器

    自ai技术的发展以来,人工智能在各个领域都展现出了无限可能。在学术界,写作论文是科研人员不可避免的重要任务,然而,论文写作需要大量的时间和经验技能,而这对刚刚步入学术领域的年轻科研人员来说尤为困难。在这样的背景下,提高写作效率、降低人力成本、缩

    2024年02月13日
    浏览(34)
  • 在 VScode 终端上创建 nuxtjs 项目遇到的问题以及使用 GitHub 遇到的问题和一些个人笔记

    这篇文章是关于在vscode终端中创建 nuxtjs项目 的一些步骤,同时还包括了使用 Git、GitHub 的一些操作,以此文章作为笔记,仅供参考。(前提:已经安装nodejs、git) 关于nuxtjs、ssr、服务端渲染、nuxtjs项目结构等等相关知识点这篇文章就不多多介绍了,在后续的文章或笔记中也

    2024年02月09日
    浏览(58)
  • 记录使用nginx部署静态资源流程,以及遇到的访问静态资源404问题

    将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应用分开部署实现 动静分离 ,提高用户访问静态代码的速度,降低对后台应用访问,减轻后台服务器的压力。 这里我选择放在了 html文件夹 下,(也可以放在和html文件夹同级,或其它位置 打开 conf文件夹 打开总配置文

    2024年02月08日
    浏览(51)
  • 集群部署项目时,Spring Task的坑大家一定要注意

    我们要在下面的代码中,实现每5秒钟执行一个打印信息的任务。 首先我们可以通过“Edit Configurations”,增加一个运行实例的配置。 接着启动项目的多个实例即可,如下图所示: 然后我们就可以查看上面代码的执行结果了,如下图: 通过对比执行结果,我们会发现,有两个

    2024年02月12日
    浏览(25)
  • 今天给大家介绍一篇基于springboot的医院管理系统的设计与实现

    临近学期结束,你还在做java程序网络编程,期末作业,老师的作业要求觉得大了吗?不知道毕业设计该怎么办?网页功能的数量是否太多?没有合适的类型或系统?等等。这里根据疫情当下,你想解决的问题,今天给大家介绍一篇基于springboot的医院管理系统的设计与实现。 随着科

    2023年04月14日
    浏览(66)
  • 免费快速部署ChatGPT线上聊天网页:ChatGPT API + Github + Railway

    (1)需要自己生成的 openai api ,获取API的网站:openAI API 获取方式:OpenAI的API key获取方法 (2)本次使用该参考项目进行部署:chatweb 需要将该项目 fork 到自己的仓库里 (3)将上述项目在Railway上部署:railway 进入后,使用github账号登录并与之关联 使用 Deploy from GitHub repo 创建

    2024年02月06日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包