How to convert this code to use the Worker service

Hello everyone,
I would like to use the Worker service
currently my code looks like this, it handles calls from a javascript client, and the server reads and writes a json file locally.

is it possible to convert this code or use it with Worker? I have a doubt about reading and writing to a file, maybe Worker doesn’t allow it.

var port = process.env.PORT || 3000,
    express = require("express"),
    app = express();
var fs = require('fs');
var filename = 'shorturl.json';
var cors = require('cors');

app.use(cors({ origin: '*' }));
app.use(express.json());'/writeJson', function(request, res) {
    fs.readFile(filename, 'utf-8', function(err, data) {
        data = JSON.parse(data);
        var obj = {
            concatURL: request.body.user.concatURL,
            chain: request.body.user.chain

        if (!err) {
            fs.writeFile(filename, JSON.stringify(data), (err) => {
                if (err) {
                    throw err;
        } else {
            throw err;
});'/readJson', function(request, response) {
    let rawdata = fs.readFileSync('shorturl.json');

app.listen(port, function() {
1 Like

Hi there - have you looked at Workers KV for this purpose? That will allow you to persist data that workers can read from and write to:

thanks, I’ll take a look


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.