I am developing a basic converter program in node.js and HTML: https://replit.com/@OSoft/Converter?v=1
It looks like it converts, but the converted file cannot be opened and is unreadable, which compels me to conclude that the conversion process isn’t actually working. I am using “libreoffice-convert”. Can anyone analyze the code and tell me what I should fix or what I am doing wrong?
I saw that you are using “done” in your index.js and you should not use the done
variable (which is the buffer) as a file path.
I modified your /convert route in your index.js
. Try to see if it works.
import fs from 'fs';
// ...
app.post('/convert', upload.single('file'), (req, res) => {
const format = req.body.format;
if (!format) {
res.status(400).send('No format specified');
return;
}
if (!req.file) {
res.status(400).send('No file uploaded');
return;
}
const inputFile = req.file.path;
const outputFile = `${inputFile}.${format}`;
convert(inputFile, format, undefined, (err, done) => {
if (err) {
console.error(err);
res.status(500).send('Error converting file');
return;
}
// Set the content type based on the desired output format
res.setHeader('Content-Type', `application/${format}`);
// Send the buffer directly to the client
res.send(done);
// Clean up the temporary file
fs.unlink(inputFile, (err) => {
if (err) {
console.error(err);
}
});
});
});
2 Likes
import express from 'express';
import multer from 'multer';
import { convert } from 'libreoffice-convert';
import { join } from 'path';
import fs from 'fs';
const app = express();
const upload = multer({ dest: 'uploads/' });
// Define a route for the root URL
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'index.html'));
});
app.post('/convert', upload.single('file'), (req, res) => {
const format = req.body.format;
if (!format) {
res.status(400).send('No format specified');
return;
}
if (!req.file) {
res.status(400).send('No file uploaded');
return;
}
const inputFile = req.file.path;
const outputFile = `${inputFile}.${format}`;
convert(inputFile, format, undefined, (err, done) => {
if (err) {
console.error(err);
res.status(500).send('Error converting file');
return;
}
// Set the content type based on the desired output format
res.setHeader('Content-Type', `application/${format}`);
// Send the buffer directly to the client
res.send(done);
// Clean up the temporary file
fs.unlink(inputFile, (err) => {
if (err) {
console.error(err);
}
});
});
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
I got this error:
import express from 'express';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 Like
Honestly I would just use require
:
const express = require('express');
2 Likes
You didn’t set the type to module in your package.json file.
You can use the recommendation of @QwertyQwerty88
2 Likes
const express = require('express');
const multer = require('multer');
import { convert } from 'libreoffice-convert';
import { join } from 'path';
import fs from 'fs';
const app = express();
const upload = multer({ dest: 'uploads/' });
// Define a route for the root URL
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'index.html'));
});
app.post('/convert', upload.single('file'), (req, res) => {
const format = req.body.format;
if (!format) {
res.status(400).send('No format specified');
return;
}
if (!req.file) {
res.status(400).send('No file uploaded');
return;
}
const inputFile = req.file.path;
const outputFile = `${inputFile}.${format}`;
convert(inputFile, format, undefined, (err, done) => {
if (err) {
console.error(err);
res.status(500).send('Error converting file');
return;
}
// Set the content type based on the desired output format
res.setHeader('Content-Type', `application/${format}`);
// Send the buffer directly to the client
res.send(done);
// Clean up the temporary file
fs.unlink(inputFile, (err) => {
if (err) {
console.error(err);
}
});
});
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
And I got this error:
import { convert } from 'libreoffice-convert';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Change
import { convert } from 'libreoffice-convert';
import { join } from 'path';
import fs from 'fs';
With
const { convert } = require('libreoffice-convert');
const { join } = require('path');
const fs = require('fs');
You are mixing ES6 import statements with CommonJS.
2 Likes