2
\$\begingroup\$

I've been working on a project to make myself more comfortable with NodeJS, especially Express and TypeScript. Searching and reviewing a lot of approaches of the people, providing bootstraps for it, I was merely satisfied with the results I've found (including the git repos from Microsoft). So I started with the Express generator and started to convert it to a TypeScript conform structure. I moved the root/bin folder with it's "www" file to root/src/bin and renamed the file "index.ts".

Now the problem I faced was that the port variable, which was initialized in the outer function, has lead tslint to mark the port variable inside the normalizePort function as "no-shadowed-variable". That's when I decided to extract the normalizePort function, as well as the event handlers onError and onListening into modules at the same directory. I'd like to get some advice, if the conversion of the variables (var -> let, const) and the modularization seems reasonable to you.

Please let me know if you want me to also include the other two modules.

Please keep in mind that I'm fairly new to programming in general and haven't had the opportunity to work on a project in a production environment.

The whole project until now can be found here.

tslint.json:

{
  "defaultSeverity": "error",
  "extends": [
      "tslint:recommended"
  ],
  "jsRules": {},
  "rules": {
    "quotemark": [true, "single"],
    "trailing-comma": [ false ]
  },
  "rulesDirectory": []
}

root/src/bin/index.ts:

#!/usr/bin/env node
'use strict';

/**
 * Module dependencies.
 * @private
 */

import { createServer } from 'http';
import app from '../app';
import { onError, onListening } from './expressHandlers';
import { normalizePort } from './normalizePort';

/**
 * Get port from environment and store in Express.
 */

const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

const server = createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Module exports.
 * @public
 */

export {
  port,
  server
};

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.