admin管理员组文章数量:1355532
Note: I converted my expressjs code to be an ES6 module. The piler hasn't plained yet and I'm surprised. I assume we can do this without having to use an .mjs file extension or put type module in a package.json inside my server folder?
I'm using
Node 14.4.0
typescript: 3.9.7
Anyway back to my problem. Not sure how you have to type expressJS stuff, for example I get No overload matches this call
here:
I mean if it's saying there is no such callback why are people using code this way? I'm pretty sure this was valid code for app.listen to check errors like this in the past
server/server.ts
import cluster from 'cluster';
import os from 'os';
import App from './api.js';
const port = process.env.PORT || 3000;
if (cluster.isMaster) {
for (let i = 0; i < os.cpus().length; i++) {
cluster.fork();
}
console.log('Ready on port %d', port);
} else {
App.listen(port, (err) => {
console.log(`express is listening on port ${port}`);
if (err) {
console.log('server startup error');
console.log(err);
}
});
}
server/api.ts
import historyApi from 'connect-history-api-fallback';
import pression from 'pression';
import countryTable from './data/countries.json';
import express from 'express';
import panyTable from './data/panies.json';
import _ from 'lodash';
const App = express()
.use(pression())
.on('error', (err: any) => {
console.log(err);
})
.get('/api/v1/countries', (_req: any, res: any) => {
res.json(countryTable.map((country: any) => _.pick(country, ['id', 'name', 'images'])));
})
.get('/api/v1/panies', (_req: any, res: any) => {
res.json(
panyTable.map((pany: any) =>
_.pick(pany, [
'id',
'active',
'images',
'locations',
])
)
);
})
.use(historyApi())
.use(express.static('dist'))
.use((_req: any, res: any) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,OPTIONS');
res.header(
'Access-Control-Allow-Headers',
'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'
);
res.send('Sorry, Page Not Found');
});
export default App;
server/tsconfig.json
{
"extends": "../../tsconfig",
"pilerOptions": {
"outDir": "../../dist/server", /* Redirect output structure to the directory. */
"rootDir": "." /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
},
"include": ["./*.ts"],
"resolveJsonModule": true
}
./tsconfig.json
{
"pilerOptions": {
/* Visit .json to read more about this file */
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "es2020", /* Specify module code generation: 'none', 'monjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["es5", "es6", "dom"], /* Specify library files to be included in the pilation. */
"moduleResolution": "node",
"allowJs": true, /* Allow javascript files to be piled. */
// "checkJs": true, /* Report errors in .js files. */
"jsx": "react",
"noImplicitAny": true,
"sourceMap": false, /* Generates corresponding '.map' file. */
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"removeComments": true, /* Do not emit ments to output. */
"strict": true, /* Enable all strict type-checking options. */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
// "rootDirs": ["."], /* List of root folders whose bined content represents the structure of the project at runtime. */
"typeRoots": [
"node_modules/@types"
], /* List of folders to include type definitions from. */
"esModuleInterop": true,
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
"resolveJsonModule": true,
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true
},
"include": [
"src"
],
"exclude": [
"/node_modules",
"/src/server",
"/src/client/js/ink-config.js",
"**/test",
"dist"
]
}
Note: I converted my expressjs code to be an ES6 module. The piler hasn't plained yet and I'm surprised. I assume we can do this without having to use an .mjs file extension or put type module in a package.json inside my server folder?
I'm using
Node 14.4.0
typescript: 3.9.7
Anyway back to my problem. Not sure how you have to type expressJS stuff, for example I get No overload matches this call
here:
I mean if it's saying there is no such callback why are people using code this way? I'm pretty sure this was valid code for app.listen to check errors like this in the past
server/server.ts
import cluster from 'cluster';
import os from 'os';
import App from './api.js';
const port = process.env.PORT || 3000;
if (cluster.isMaster) {
for (let i = 0; i < os.cpus().length; i++) {
cluster.fork();
}
console.log('Ready on port %d', port);
} else {
App.listen(port, (err) => {
console.log(`express is listening on port ${port}`);
if (err) {
console.log('server startup error');
console.log(err);
}
});
}
server/api.ts
import historyApi from 'connect-history-api-fallback';
import pression from 'pression';
import countryTable from './data/countries.json';
import express from 'express';
import panyTable from './data/panies.json';
import _ from 'lodash';
const App = express()
.use(pression())
.on('error', (err: any) => {
console.log(err);
})
.get('/api/v1/countries', (_req: any, res: any) => {
res.json(countryTable.map((country: any) => _.pick(country, ['id', 'name', 'images'])));
})
.get('/api/v1/panies', (_req: any, res: any) => {
res.json(
panyTable.map((pany: any) =>
_.pick(pany, [
'id',
'active',
'images',
'locations',
])
)
);
})
.use(historyApi())
.use(express.static('dist'))
.use((_req: any, res: any) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,OPTIONS');
res.header(
'Access-Control-Allow-Headers',
'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'
);
res.send('Sorry, Page Not Found');
});
export default App;
server/tsconfig.json
{
"extends": "../../tsconfig",
"pilerOptions": {
"outDir": "../../dist/server", /* Redirect output structure to the directory. */
"rootDir": "." /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
},
"include": ["./*.ts"],
"resolveJsonModule": true
}
./tsconfig.json
{
"pilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "es2020", /* Specify module code generation: 'none', 'monjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["es5", "es6", "dom"], /* Specify library files to be included in the pilation. */
"moduleResolution": "node",
"allowJs": true, /* Allow javascript files to be piled. */
// "checkJs": true, /* Report errors in .js files. */
"jsx": "react",
"noImplicitAny": true,
"sourceMap": false, /* Generates corresponding '.map' file. */
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"removeComments": true, /* Do not emit ments to output. */
"strict": true, /* Enable all strict type-checking options. */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
// "rootDirs": ["."], /* List of root folders whose bined content represents the structure of the project at runtime. */
"typeRoots": [
"node_modules/@types"
], /* List of folders to include type definitions from. */
"esModuleInterop": true,
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
"resolveJsonModule": true,
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true
},
"include": [
"src"
],
"exclude": [
"/node_modules",
"/src/server",
"/src/client/js/ink-config.js",
"**/test",
"dist"
]
}
Share
Improve this question
edited Aug 9, 2020 at 3:51
PositiveGuy
asked Aug 8, 2020 at 5:13
PositiveGuyPositiveGuy
20.3k30 gold badges94 silver badges152 bronze badges
6
- what's the source for api.js? also, why this module does not target TypeScript? – Mario Vernari Commented Aug 8, 2020 at 5:44
- what do you mean not target typescript? what do you mean by source for the API? – PositiveGuy Commented Aug 8, 2020 at 22:17
-
the problem is on the
listen
method of theapp
instance, which es from the api.js module. So, I wondering how is the code of this method. Moreover, why the module is named api.js (which defines JavaScript), and not a simple "api" (without any extension), which implicitly defines a TypeScript module? – Mario Vernari Commented Aug 9, 2020 at 2:33 - let me post api.js so you understand why. I decouple my api logic from the simple serving of it, which is mon. – PositiveGuy Commented Aug 9, 2020 at 3:49
- @MarioVernari check out the update, it shows api.js – PositiveGuy Commented Aug 9, 2020 at 3:51
2 Answers
Reset to default 6The error is telling you that the listen()
callback does not take any parameters. The correct code should be:
App.listen(port, () => {
console.log(`express is listening on port ${port}`);
});
Basically delete the error parameter (err) and anything related to it because it does not exist.
The error is instead caught by the on('error')
method. But you have already defined it so you should be OK.
You can't quite print out just "server startup error" because the on('error')
method catches all errors, not just server startup. But you can catch and display specific errors:
// in api.ts:
.on('error', (err: any) => {
if (e.code === 'EADDRINUSE') {
console.log('server startup error: address already in use');
}
else {
console.log(err);
}
})
This is correct and is not a bug in the express types for typescript. Express merely calls node's http.Server.listen()
which in turn calls net.Server.listen()
whose callback really does not pass in an error parameter.
For a list of system errors such EADDRINUSE
as see: https://nodejs/api/errors.html#errors_mon_system_errors
For a list of node.js specific errros see: https://nodejs/api/errors.html#errors_node_js_error_codes
I fixed my own my downgrade typescript version
"typescript": "5.1.6"
本文标签: javascriptTypeScript Error for expressJS No overload matches this callStack Overflow
版权声明:本文标题:javascript - TypeScript Error for expressJS: No overload matches this call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743937691a2564982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论