Docker image pandoc/core from a Node.js Express application

To call the pandoc/core Docker image from a Node.js Express application, you can use the dockerode library, which is a Docker client for Node.js. This allows you to interact with Docker from within your Node.js application. Below is an example of how you can set this up.

Step-by-Step Guide

  1. Install Required Packages:
    First, create a new Node.js project and install the necessary packages.

    mkdir pandoc-express
    cd pandoc-express
    npm init -y
    npm install express dockerode body-parser
    
  2. Set Up the Express Application:
    Create an Express application in app.js with endpoints to handle document conversions.

    // app.js
    const express = require('express');
    const bodyParser = require('body-parser');
    const Docker = require('dockerode');
    const fs = require('fs');
    const path = require('path');
    
    const app = express();
    const docker = new Docker();
    
    app.use(bodyParser.json());
    
    // Endpoint to convert Markdown to PDF
    app.post('/convert', (req, res) => {
        const { inputMarkdown, outputFormat } = req.body;
        const inputFilePath = path.join(__dirname, 'input.md');
        const outputFilePath = path.join(__dirname, `output.${outputFormat}`);
        
        // Write the input Markdown to a file
        fs.writeFileSync(inputFilePath, inputMarkdown);
    
        // Run the pandoc Docker container
        docker.run('pandoc/core', ['-f', 'markdown', '-t', outputFormat, '/data/input.md', '-o', `/data/output.${outputFormat}`], process.stdout, {
            Binds: [`${__dirname}:/data`]
        }, (err, data, container) => {
            if (err) {
                return res.status(500).send(err);
            }
            // Read the output file
            fs.readFile(outputFilePath, (err, data) => {
                if (err) {
                    return res.status(500).send(err);
                }
                res.setHeader('Content-Disposition', `attachment; filename=output.${outputFormat}`);
                res.send(data);
    
                // Clean up the files
                fs.unlinkSync(inputFilePath);
                fs.unlinkSync(outputFilePath);
            });
        });
    });
    
    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () => {
        console.log(`Server is running on port ${PORT}`);
    });
    
  3. Run the Express Application:
    Start the Express application using Node.js.

    node app.js
    
  4. Make a Request to the Endpoint:
    You can use a tool like curl, Postman, or an HTTP client in your browser to make a POST request to the /convert endpoint.

    Example using curl:

    curl -X POST http://localhost:3000/convert \
        -H "Content-Type: application/json" \
        -d '{"inputMarkdown": "# Hello World", "outputFormat": "pdf"}' \
        --output output.pdf
    

Explanation

  • Dependencies: The express package is used to create the web server, dockerode to interact with Docker, and body-parser to parse JSON request bodies.
  • Docker Container: The Docker container is run with the pandoc/core image to convert the Markdown file to the desired output format.
  • File Handling: The input Markdown is written to a file in the server directory, and the output file is read back after conversion. Both files are cleaned up after the response is sent.

This setup allows your Express application to leverage the pandoc/core Docker image to convert documents dynamically.

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 12:34:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 12:34:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 12:34:05       82 阅读
  4. Python语言-面向对象

    2024-06-08 12:34:05       91 阅读

热门阅读

  1. 04Docker网络基础配置

    2024-06-08 12:34:05       31 阅读
  2. docker_如何推送镜像到仓库(hub.docker.com)

    2024-06-08 12:34:05       24 阅读
  3. psql导入数据报错排查

    2024-06-08 12:34:05       31 阅读
  4. 鸿蒙 Harmony ArkTs开发教程三 流程控制

    2024-06-08 12:34:05       19 阅读
  5. Hatch 现代化的项目管理、构建工具

    2024-06-08 12:34:05       22 阅读
  6. webpack

    2024-06-08 12:34:05       34 阅读
  7. Android的SELinux详解

    2024-06-08 12:34:05       31 阅读
  8. 高精度加法与高精度乘法

    2024-06-08 12:34:05       34 阅读
  9. STM32F103 点亮LED闪烁与仿真

    2024-06-08 12:34:05       23 阅读
  10. 在Linux平台下使用 .NET Core技术的UI方案

    2024-06-08 12:34:05       31 阅读
  11. Git⾯试真题(10题)

    2024-06-08 12:34:05       24 阅读
  12. 笔记:Mysql的安全策略

    2024-06-08 12:34:05       29 阅读
  13. Spring (45)Gateway

    2024-06-08 12:34:05       30 阅读
  14. docker架构

    2024-06-08 12:34:05       31 阅读
  15. CSS隐藏元素的方法

    2024-06-08 12:34:05       30 阅读
  16. 数据分析------统计学知识点(二)

    2024-06-08 12:34:05       27 阅读
  17. 用ansible部署k8s --- kubespray源码详解(一)

    2024-06-08 12:34:05       19 阅读
  18. websocket 前端项目js示例

    2024-06-08 12:34:05       30 阅读