简体   繁体   English

将一个简单的 React 表单应用程序添加到现有的 Nodejs docker 图像

[英]Add a simple React form application to an existing Nodejs docker image

I have created a Dockerfile, when run with docker-compose displays 'Hello world' in the browser.我创建了一个 Dockerfile,当与 docker-compose 一起运行时,浏览器中会显示“Hello world”。 Now I want to be able to display a simple react form with some fields to enter text in and a submit button.现在我希望能够显示一个简单的反应表单,其中包含一些用于输入文本的字段和一个提交按钮。 Is there examples of any simple projects out there that I can just use.是否有我可以使用的任何简单项目的示例。 How will I update my files, that I have shown below我将如何更新我在下面显示的文件

These are the files that I cam using:这些是我使用的文件:

Package.json - Package.json -

 {
  "name": "nodejs-hello",
  "version": "1.0.0",
  "description": "des",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "hh",
  "license": "ISC",
  "keywords": [
    "h"
  ],
  "dependencies": {
    "express": "^4.17.1"
  }
}

index.js - index.js -

    //now it load express module with `require` directive
var express = require('express')
var app = express()
const PORT = 8080;
//Define request response in root URL (/) and response with text Hello World!
app.get('/', function (req, res) {
  res.send('Hello World')
})
//Launch listening server on port 8080 and consoles the log.
app.listen(PORT, function () {
  console.log('app listening on port ' + PORT)
})

Dockerfile - Dockerfile -

FROM node:latest

WORKDIR /app

COPY package.json index.js ./

RUN npm install

EXPOSE 8080

CMD node index.js

Docker Compose - Docker 组成 -

version: "3"

services:
  web:
    image: my-image:1.0
    build: .
    ports:
      - '8080:8080'

https://medium.com/faun/a-simple-docker-setup-for-simple-hello-world-nodejs-application-bcf79bb608a0 https://medium.com/faun/a-simple-docker-setup-for-simple-hello-world-nodejs-application-bcf79bb608a0

What I have now is similar to the example in that link.我现在拥有的类似于该链接中的示例。

Now I just want to be able to display a react form in the browser.现在我只想能够在浏览器中显示一个反应表单。

Just create your React app and then add this line to copy the src files into the container:只需创建您的 React 应用程序,然后添加此行以将 src 文件复制到容器中:

COPY . .

Also, change the CMD (if you're using a CRA) to this:此外,将CMD (如果您使用的是 CRA)更改为:

CMD [ "npm", "start" ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM