2017-03-06 199 views
-1

我正在開發一個Angular2應用程序,我正在爲它構建一個簡單的Web服務。因此我想在服務器上使用Typsecript。是否有標準的Typescript + Nodejs服務器樣板模板?

我在網上看了一下這個,發現只有一些博客文章用Node和Express手動設置Typsecript。

我想知道是否有一個標準樣板模板,可以簡單地使用Typescript和Hello World Web服務在節點(也許快車)啓動項目。

編輯:我想,問題的一個不同的旋轉將是:是否有一個Web框架生成Typescript?編輯2:我無法找到一個正確的答案,所以我最終擺脫了Typescript,並在Sails與一個完整的框架,並只使用他們的Javascript版本。

+1

Downvoter,請評論。 –

回答

1

針對Visual Studio的Nodejs工具附帶一對Express啓動器模板。不幸的是,他們使用舊版本的Express,並不是一個好的開始。

將TypeScript應用於任何問題的最佳方式是從一些JavaScript開始。所以任何Express模板項目都可以做得很好,如果您使用的是Visual Studio代碼或其他一些IDE,則會自動爲您安裝類型。

或只是npm i --save @types/express

此外,通過將JavaScript/TypeScript前端與其他後端技術一起使用NodeJS後端,您也沒有任何收穫。序列化屏障從字面上封裝了它們,所以只需使用後端最有意義的東西即可。

如果您打算共享模型的接口聲明,有很多工具可以從其他語言源文件生成TypeScript。

+0

我只是想建立一個簡單的web服務。我認爲從我聽到的情況來看,Node服務應該比其他技術更簡單。因此,爲什麼我認爲這將是微型服務的不錯選擇。 –

+0

如果你真的只想要一個API來測試你的應用程序,你可以使用像npm:json-server這樣的東西,它可以讓你將文件託管在任意目錄之外並向它們發出HTTP請求。這只是爲了測試,但它不是很好。 –

+1

Downvoter請評論。 –

0

https://github.com/dwyl/hapi-typescript-example

我也開始一個例子帆typoescript應用https://github.com/aslanvaroqua/sails-ts

基本要點是:

/** 
* WelcomeController 
* 
* @description :: Server-side logic for managing Welcomes 
* @help  :: See http://links.sailsjs.org/docs/controllers 
*/ 

import e = require('express'); 
import util = require('util'); 

declare const sails: any; 

const WelcomeController = { 
    index: function (req: e.Request, res: e.Response, next: Function) { 
    console.log('index() from WelcomeController.ts'); 
    sails.models.welcome.find().limit(1).then((welcome) => { 
     /// TODO: add logger 
     console.log(`welcome page rendering w/ message ${welcome[0].message}`); 
     return res.render('welcome', { 
     welcome: welcome[0].message 
     }); 
    }).catch((err:Error) => { 
     console.error(err.message); 
     return res.render('500', err) 
    }); 


    }, 
    config: function (req: e.Request, res:e.Response, next:Function) { 
    console.log('config() from WelcomeController.ts'); 
    return res.status(200) 
     .send('<h1>sails.config :</h1><pre>' + util.inspect(sails.config) + '<pre>'); 
    } 
}; 

module.exports = WelcomeController; 

模型

export class Welcome { 
    attributes: any = { 
    id: { 
     type: 'integer', 
     primaryKey: true 
    }, 
    message: { 
     type: 'string', 
     required: true, 
     defaultsTo: 'default message' 
    } 
    }; 
} 

typings.json

{ 
    "dependencies": { 
    "bluebird": "registry:npm/bluebird#3.5.0+20170314181206", 
    "connect": "registry:dt/connect#3.4.0+20160510010627", 
    "express": "registry:dt/express#4.0.0+20170118060322", 
    "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20170324160323", 
    "sails": "registry:npm/sails#0.12.0+20160610190623", 
    "serve-static": "registry:dt/serve-static#1.7.1+20161128184045" 
    }, 
    "globalDependencies": { 
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160726072212", 
    "node": "registry:dt/node#7.0.0+20170322231424", 
    "socket.io": "registry:dt/socket.io#1.4.4+20170313110830" 
    } 
} 

測試

describe("HashSet", function() { 
    it("should behave like a set, removing duplicates", function() { 

    }); 
}); 
相關問題