I'm trying to write a middleware of express. And I wrote that in a single file test2.js
In the server, I can have intellisense like:
In that single file, the middleware works fine, but I can't have intellisense of req and res
Is there any way to get the intellisense?
Here is my server test1.js:
//test1.js
let http = require("http");
let express = require("express");
let app = express();
let middle = require("./test2.js");
app.use(middle);
app.use(function(req, res, next) {
next();
});
http.createServer(app).listen(80);
Here is my middleware test2.js:
//test2.js
module.exports = function(req, res, next) {
console.log("middle");
next();
};
To access this options page, choose Tools > Options, and then choose Text Editor > C# > IntelliSense.
If the IntelliSense is installed and still not working then most of the time restarting/reloading the program will solve the issue. So give it a try. Step 1: To restart VS Code open VS Code and press Ctrl + Shift + P keys together to open the command palette and type Reload Window in the search.
Maybe JSDoc is an option?
You might need to install the type definitions: npm i @types/express -D
("Automatic Type Acquisition" in VS Code may or may not do that for your automatically)
/** @type {import("express").RequestHandler} */
module.exports = function (req, res, next) {
req. // intellisense works
next();
}
https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript
myTypes.d.ts
import Express from "express";
declare global {
type RequestHandler = Express.RequestHandler;
}
Types are usually bound to the module's scope, but you can import any type and re-declare it in the global scope.
Now vscode finds the types without the "dirty" {import("express")}
myMiddleware.js
/** @type RequestHandler */
module.exports = function (req, res, next) {
req. // intellisense works
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With