Reputation: 33
I'm wanting to use typescript in my node/express environment, hosting in the Cloud 9 ide.
I've got a problem trying to get the compiler to compile app.ts It comes up with several errors of which Property 'bodyParser' does not exist on type 'typeof e' is one of them
I have several definition files in the root folder of the application, namely express.d.ts, node.d.ts, body-parser.d.ts. I added body-parser.d.ts in desperation thinking the body parser error would be solved.
The command line is: tsc --sourcemap --module commonjs app.ts The code in app.ts is as follows:
// Import express with body parsers (for handling JSON)
import express = require('express');
var bodyParser = require('body-parser');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var expressValidator = require('express-validator');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(expressValidator([]));
// add session support!
app.use(express.cookieParser());
app.use(express.session({ secret: 'sauce' }));
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// Uncommend this line to demo basic auth
// app.use(express.basicAuth((user, password) => user == "user2" && password == "password"));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
function restrict(req, res, next) {
if (req.session.authenticated) {
next();
} else {
res.redirect('/');
}
}
// Get
app.get('/', routes.index);
app.get('/login', routes.login);
app.get('/register', routes.register);
app.get('/users', user.list);
app.get('/AddPlayer', routes.addPlayer);
app.get('/dashboard', restrict, routes.dashboard);
app.get('/logout', routes.logout);
// POST
app.post('/AddPlayer', routes.AddPlayer);
app.post('/login', routes.loginUser);
app.post('/register', routes.registerUser);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Upvotes: 2
Views: 8323
Reputation: 67
I dont think you should be using TSD , typings is the new standard . And you can get it using npm . For getting express through typings , you can use these commands then to install all dependencies for express
typings install express --ambient --save
typings install serve-static --ambient --save
typings install express-serve-static-core --ambient --save
typings install mime --ambient --save
Upvotes: 2
Reputation: 2858
You didn't add any type information to your ts file
the error 'typeof e'
is typeof express which is declared but has no type.
express in this case is simply the identifier, the name of the variable.
The only Typescript feature you used is the import statement but it might not be needed in that case
I'll go with a var, and forget about TS module definitions.
simply var express = require('express')
Instead of throwing type definitions in your root folder, try using TSD or Nuget if you are on Windows ( I'll strongly recommend TSD over Nuget) .
This will bring some order to your project and things will start to make sense. tsd makes it very easy for you start with
On command line
npm init
npm install tsd --save-dev
#you couldalso install it globally first , with
# npm install tsd -g
# once done, initialize it
tsd init
#then get the definitions you need
tsd query node express --action install --save
tsd will place the definitions under typings\tsd.d.ts for you
then you can add the reference to your script
/// <reference path='typings/tsd.d.ts' />
now its time to add the type info
It Still won't work but at least it will compile :)
It the first time have look at the express definition file and I have the feeling it might not help much,. Express has changed bit lately and Im not sure how accurate can the definition file be. and on top of that Express is quite dynamic, the d.ts is full of any's .. :) I'm not sure if you will get any real gain from the type definitions with express but You can still get some nice features out Typescript and build your types/interfaces or extend the existing ones with what you are expecting .
Upvotes: 1