首页 > 解决方案 > 使用 TypeScript 构建 RESTful API 时出现“系统未定义”

问题描述

我正在尝试使用 TypeScript 和 SystemJS 构建 RESTful API

// index.ts
// Import express
import express from "express"
// Imports routes
import apiRoutes from "api-routes.js"

// Initialise the app
const app = express()

// set up server port
const port = process.env.port || 3000

// Send message for default URL
app.get('/', (request, response) => response.send('Hello World with Express.'))

// Launch app to listen to specified port
app.listen(port, function(){
    console.log(`Listening on port ${port}`)
})

// Use API routes in the app.
app.use('/api', apiRoutes)

该文件被编译为 javascript

System.register(["express", "api-routes.js"], function (exports_1, context_1) {
    "use strict";
    var express_1, api_routes_js_1, app, port;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (express_1_1) {
                express_1 = express_1_1;
            },
            function (api_routes_js_1_1) {
                api_routes_js_1 = api_routes_js_1_1;
            }
        ],
        execute: function () {
            // import * as express from "express"
            // import * apiRoutes from './build/'
            // Initialise the app
            app = express_1.default();
            // set up server port
            port = process.env.port || 3000;
            // Send message for default URL
            app.get('/', function (request, response) { return response.send('Hello World with Express.'); });
            // Launch app to listen to specified port
            app.listen(port, function () {
                console.log("Listening on port " + port);
            });
            // Use API routes in the app.
            app.use('/api', api_routes_js_1.default);
        }
    };
});

我收到以下错误

在此处输入图像描述

它说我无法system从系统模块中引用。这是我的仓库的链接

html我像这样导入 SystemJS

<script src="node_modules/systemjs/dist/system.js"></script>

如何在我的index.ts文件中导入 SystemJS?

标签: javascripttypescriptsystemjs

解决方案


推荐阅读