// express server + typescript see: http://brianflove.com/2016/11/08/typescript-2-express-node/ var DEFAULT_PORT = 3001; // get from config var DEFAULT_HOSTNAME = "127.0.0.1"; var filename = "src/testdata/test_data.ts"; import TESTS from '../test/minimal-testsuites' import TEST_2 from '../test/TEST_2' import * as bodyParser from "body-parser"; import * as express from "express"; import * as http from 'http' import { Application } from "express"; import errorHandler from 'errorhandler' import path from 'path' import { saveFile } from "./file-util"; import { IndexRoute } from "./server/IndexRouter"; import { ArchiveDeserializer } from '@cafetextual/util'; function toCls(data:string) { var out = 'var GRAMMAR_TEST_DATA = ' + data + "\n export default GRAMMAR_TEST_DATA"; return out; } export default class ParseServer { constructor(config:any) { this._config = config //create expressjs application this.app = express(); //configure application this.config(); //add routes this.routes(); //add api this.api(); } _config:any app:Application; config() { this.app.use(bodyParser.json()); // to support JSON-encoded bodies this.app.use(bodyParser.urlencoded({ // to support URL-encoded bodies extended: true })); var server; // Start the app if not loaded by another module //if (!module.parent) { server = http.createServer(this.app); server.listen( //config.port || DEFAULT_PORT, //config.hostname || DEFAULT_HOSTNAME, () => { console.log(' started server to get receive test data on localhost:3001/save'); }); var lastData = " no data yet"; this.app.get('/', (req, res) => { res.send('last data:\n ' + lastData); }); this.app.get('/testsuite' ,(req, res) => { var serializer:ArchiveDeserializer console.log(' serveing testsuite') var out:string = JSON.stringify(TEST_2) res.setHeader('Content-Type', 'application/json'); res.setHeader('Access-Control-Allow-Origin','*'); // <--- for dbug from browsers res.send(out); }) this.app.post('/parse', (req, res) => { var data = req.body.data; // still stringified json console.log(' content to parse ' + data); // do parse here }) this.app.put('/save', (req, res) => { var data = req.body.data; // still stringified json console.log(' posted data ' + data); var clsTxt = toCls(data) saveFile("./" + filename, clsTxt); lastData = data; res.send('Posted some data' + data); }); // module.exports = app; // ---- parse on demand api this.app.post('/parse', (req, res) => { } ) } // public config_old() { //add static paths // this.app.use(express.static(path.join(__dirname, "public"))); //configure pug // this.app.set("views", path.join(__dirname, "views")); // this.app.set("view engine", "pug"); //use logger middlware //his.app.use(logger("dev")); //use json form parser middlware //this.app.use(bodyParser.json()); //use query string parser middlware //this.app.use(bodyParser.urlencoded({ // extended: true // })); //use cookie parser middleware //this.app.use(cookieParser("SECRET_GOES_HERE")); //use override middlware // this.app.use(methodOverride()); //catch 404 and forward to error handler // this.app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) { // err.status = 404; // next(err); // }); //error handling // this.app.use(errorHandler(), () => { // this.api() // } /** * Create router. * * @class Server * @method config * @return void */ private routes() { /* let router: express.Router; router = express.Router(); //IndexRoute IndexRoute.create(router); //use router middleware this.app.use(router); */ } api() { } } // ParseServer