简体   繁体   中英

What is the difference the npm packages @types/express and express

I am trying to build node server, I am confused whether to use @types/express or express.

This is my server that I want to build

'use strict';
const express = require('express');
const http = require('http');
const path = require('path');
const genuid = require("gen-uid");
const morgan = require('morgan');
const morganBody = require('morgan-body');
const bodyParser = require('body-parser');
const moment = require('moment');
const routes = require('./server/index.route');
//const config = require('./server/config/config') ;
const app = express();
morgan.token('user', (req, res) => {
  if (req.user) {
    return req.user.id;
  } else {
    return "Guest";
  }
});
morgan.token('timestamp', (req, res) => {
  return moment().format();
});
morgan.token('token', (req, res) => {
  if (!req.requestId) {
    req.requestId = "[" + genuid.token(true).substr(0, 8) + "]";
  }
  return req.requestId;
});

app.use(morgan(':token :timestamp :method HTTP/:http-version :user-agent :url :user :remote-addr ', {
  immediate: true
}));

app.use(morgan(':token :timestamp :method HTTP/:http-version :user-agent :url :user :remote-addr :status :response-time'));
app.use(bodyParser.json({ limit: '50mb' }));
morganBody(app);
app.use(bodyParser.urlencoded({ extended : false }));

app.use(express.static(path.join(__dirname, '/dist')));

const logRequestStart = (req, res, next) => {
  //console.info(`${req.requestId} ${req.method} ${req.originalUrl}`);
  console.info(`${req.requestId} ${JSON.stringify(req.body)}`); 
  res.on('finish', () => {
      //console.info(`${req.requestId} ${res.statusCode} ${res.statusMessage}; ${res.get('Content-Length') || 0}b sent`);
      //console.log(res.body); 
  })
  next()
}

app.use(logRequestStart);


let port = 4440;

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'?'Pipe ' + port:'Port ' + port;

  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'?'pipe ' + addr: 'port ' + addr.port;
  console.log('Listening on ' + bind);
}


let server;
server = http.createServer(app);



app.set('port', port);
app.set('view engine', 'ejs');


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

server.listen(port);
routes(app);
module.exports = app;

You need to use both the dependency

express and @types/express

as typescript doesn't know types of express class so @types/express helps in that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM