Home > other >  I want to know convert my code expressJS to typescript
I want to know convert my code expressJS to typescript

Time:01-08

I have simple project images api for learning but don't know how convert it to typescript

I want to know how convert my code express JS to typescript .......................

var express = require('express');
var bodyparser = require('body-parser');
var sharp = require('sharp');
const fs = require("fs"); // Or `import fs from "fs";` with ESM

var app = express();

app.use(bodyparser.urlencoded({ extended: true }))
app.use(express.static('images'));


let imgTum;
let imgTum2;
app.get('/api/images', (req, res) => {
    imgTum = `${__dirname}/images/tum/${req.query.filename}-${req.query.width}-${req.query.height}.jpg`;
    imgTum2 = `/tum/${req.query.filename}-${req.query.width}-${req.query.height}.jpg`;

    if (fs.existsSync(imgTum)) {
        res.send(`<img src="${imgTum2}" ></img>`);
        console.log('1');
    }
    else {
        sharp(`${__dirname}/images/full/${req.query.filename}.jpg`)
            .resize(parseInt(req.query.width), parseInt(req.query.height))
            .toFile(imgTum)
            .then(() => {
                res.send(`<img src="${imgTum2}" ></img>`);
                console.log('2');
            });
    }

});



app.listen(1000, () => {
    console.log("Server Running!")
})

CodePudding user response:

The beauty about NodeJs is the freedom to set up your project in just about any structure that suits your need. But this comes with its own challenges again.
You might consider looking at different typescript starter projects on Github to guide you on how to set up a typescript project.
I personally pick ideas from this Microsoft repository https://github.com/microsoft/TypeScript-Node-Starter.

CodePudding user response:

Welcome to Stack Overflow. This type of question isn't really what Stack Overflow is designed to answer. SO questions should be problem / solution oriented, specific, and show that you've attempted to solve the problem yourself. See this post for tips about how to ask a good question.

Typescript and Express.js are not mutually exclusive. You'll still use Express.js in your Typescript project when it's "converted". The first step is to learn the basics of Typescript. Next, study an Express w/ Typescript example project. Finally, start moving your code to Typescript.

  •  Tags:  
  • Related