Skip to content

Commit 98a7ae5

Browse files
Added support for typescript and sample code with express.js .
1 parent 29e8a28 commit 98a7ae5

18 files changed

+284
-12
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
.idea/
2+
src/node_modules/
3+
src/typings/
4+
src/**/*.js
5+
src/**/*.map

docker-compose-tools.yml

+10
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,15 @@ services:
99
mocha:
1010
build: ./docker/node
1111
entrypoint: mocha
12+
volumes:
13+
- ./src:/app
14+
mocha:
15+
build: ./docker/node
16+
entrypoint: mocha
17+
volumes:
18+
- ./src:/app
19+
tsc:
20+
build: ./docker/node
21+
entrypoint: tsc
1222
volumes:
1323
- ./src:/app

docker-compose.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
version: '2'
22

33
services:
4-
active-directory-rest:
4+
node:
55
build: ./docker/node
66
restart: always
77
volumes:
88
- ./src:/app
99
ports:
10-
- "8093:80"
10+
- "8093:3000"
11+
command: tsc-watch --onSuccess 'node bin/www.js'

docker/node/Dockerfile

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM node:7.8
2-
3-
RUN apt-get update & apt-get install git
1+
FROM node:7.9-alpine
42

53
RUN npm install -g mocha
6-
RUN npm install -g sails
7-
RUN npm install -g grunt
4+
RUN npm install -g typescript
5+
RUN npm install -g tsc-watch
6+
7+
WORKDIR /app
88

9-
WORKDIR /app
9+
EXPOSE 3000

src/app.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var favicon = require('serve-favicon');
4+
var logger = require('morgan');
5+
var cookieParser = require('cookie-parser');
6+
var bodyParser = require('body-parser');
7+
8+
var index = require('./routes/index');
9+
var users = require('./routes/users');
10+
11+
var app = express();
12+
13+
// view engine setup
14+
app.set('views', path.join(__dirname, 'views'));
15+
app.set('view engine', 'jade');
16+
17+
// uncomment after placing your favicon in /public
18+
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
19+
app.use(logger('dev'));
20+
app.use(bodyParser.json());
21+
app.use(bodyParser.urlencoded({ extended: false }));
22+
app.use(cookieParser());
23+
app.use(express.static(path.join(__dirname, 'public')));
24+
25+
app.use('/', index);
26+
app.use('/users', users);
27+
28+
// catch 404 and forward to error handler
29+
app.use(function(req, res, next) {
30+
var err:any = new Error('Not Found');
31+
err.status = 404;
32+
next(err);
33+
});
34+
35+
// error handler
36+
app.use(function(err, req, res, next) {
37+
// set locals, only providing error in development
38+
res.locals.message = err.message;
39+
res.locals.error = req.app.get('env') === 'development' ? err : {};
40+
41+
// render the error page
42+
res.status(err.status || 500);
43+
res.render('error');
44+
});
45+
46+
module.exports = app;

src/bin/www.ts

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('skeleton:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

src/index.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as express from "express";
2+
import * as bodyParser from "body-parser";
3+
import * as cookieParser from "cookie-parser";
4+
import * as logger from "morgan";
5+
import * as fs from "fs";
6+
import * as path from "path";
7+
import errorHandler = require("errorhandler");
8+
import methodOverride = require("method-override");
9+
10+
function configApp(app) {
11+
app.use(logger('common', {
12+
stream: fs.createWriteStream(path.join(__dirname, 'logs/access.log'), {flags: 'a'})
13+
}));
14+
app.use(bodyParser.json());
15+
app.use(bodyParser.urlencoded({extended: true}));
16+
app.use(express.static(path.join(__dirname, 'public')));
17+
app.use(cookieParser("SECRET_GOES_HERE"));
18+
app.use(methodOverride());
19+
app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
20+
err.status = 404;
21+
next(err);
22+
});
23+
app.use(errorHandler());
24+
}
25+
26+
const PORT = 3000;
27+
const app : express.Express = express();
28+
configApp(app);
29+
30+
app.get('/', function (req: express.Request, res: express.Response) {
31+
res.json({hello:'world'});
32+
});
33+
34+
app.listen(PORT, function () {
35+
const msg = `active-directory-rest active on port ${PORT}`;
36+
console.log(msg)
37+
});

src/logs/.gitkeep

Whitespace-only changes.

src/logs/access.log

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
::ffff:172.25.0.1 - - [14/Apr/2017:14:19:51 +0000] "GET / HTTP/1.1" 304 -
2+
::ffff:172.25.0.1 - - [18/Apr/2017:17:24:53 +0000] "GET / HTTP/1.1" 304 -
3+
::ffff:172.25.0.1 - - [18/Apr/2017:17:24:54 +0000] "GET /favicon.ico HTTP/1.1" 404 150

src/package.json

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "active-directory-rest",
33
"version": "0.0.1",
4-
"description": "REST web service to expose the content of an active directory domain.",
5-
"main": "index.js",
64
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
5+
"test": "echo \"Error: no test specified\" && exit 1",
6+
"start": "node ./bin/www.js"
87
},
98
"repository": {
109
"type": "git",
@@ -21,5 +20,28 @@
2120
"bugs": {
2221
"url": "https://github.com/iit-federico-bozzini/active-directory-rest/issues"
2322
},
24-
"homepage": "https://github.com/iit-federico-bozzini/active-directory-rest#readme"
23+
"homepage": "https://github.com/iit-federico-bozzini/active-directory-rest#readme",
24+
"dependencies": {
25+
"body-parser": "^1.17.1",
26+
"cookie-parser": "^1.4.3",
27+
"debug": "~2.6.3",
28+
"errorhandler": "^1.5.0",
29+
"express": "^4.15.2",
30+
"jade": "~1.11.0",
31+
"fs": "0.0.1-security",
32+
"jsonfile": "^2.4.0",
33+
"method-override": "^2.3.8",
34+
"morgan": "^1.8.1",
35+
"serve-favicon": "~2.4.2"
36+
},
37+
"devDependencies": {
38+
"@types/body-parser": "^1.16.3",
39+
"@types/cookie-parser": "^1.3.30",
40+
"@types/errorhandler": "0.0.30",
41+
"@types/express": "^4.0.35",
42+
"@types/jade": "0.0.30",
43+
"@types/method-override": "0.0.29",
44+
"@types/morgan": "^1.7.32",
45+
"@types/node": "^7.0.12"
46+
}
2547
}

src/public/stylesheets/style.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
padding: 50px;
3+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4+
}
5+
6+
a {
7+
color: #00B7FF;
8+
}

src/routes/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
4+
/* GET home page. */
5+
router.get('/', function(req, res, next) {
6+
res.render('index', { title: 'Express' });
7+
});
8+
9+
module.exports = router;

src/routes/users.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
4+
/* GET users listing. */
5+
router.get('/', function(req, res, next) {
6+
res.send('respond with a resource');
7+
});
8+
9+
module.exports = router;

src/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"typeRoots": [
5+
"node_modules/@types"
6+
]
7+
},
8+
"include": ["**/*.ts"],
9+
"exclude": [
10+
"node_modules",
11+
"!node_modules/@types",
12+
"**/*.spec.ts"
13+
]
14+
}

src/views/error.jade

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extends layout
2+
3+
block content
4+
h1= message
5+
h2= error.status
6+
pre #{error.stack}

src/views/index.jade

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extends layout
2+
3+
block content
4+
h1= title
5+
p Welcome to #{title}

src/views/layout.jade

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
doctype html
2+
html
3+
head
4+
title= title
5+
link(rel='stylesheet', href='/stylesheets/style.css')
6+
body
7+
block content

tsc.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker-compose -f docker-compose-tools.yml run --rm tsc %*

0 commit comments

Comments
 (0)