دنبال کننده ها

۱۳۹۶ دی ۱۲, سه‌شنبه

How to use express with gdi2290 angular-starter?

[ad_1]



I am using the following project as is:



https://github.com/gdi2290/angular-starter



My problem is I want to use some authentication library, but it requires express. What I've done is:



  1. From root directory, execute 'npm run build:prod', and I see the dist folder created with a bunch of files

  2. Create a new directory for 'server' and create a server.js file that contains:



    const express = require("express");
    const app = express();
    const bodyparser = require("body-parser");
    const json = bodyparser.json;
    const http = require('http').Server(app);
    const urlencoded = bodyparser.urlencoded;
    const path = require("path");



    app.use(json());
    app.use(urlencoded(
    extended: true
    ));
    app.use(express.static(__dirname + '/../dist'));



    app.get('/test', (req, res) =>
    /* when using webpack-dev-server we are using webpack's url
    so we need to set headers for development i.e npm run server:dev:hmr
    */
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');



    return res.json(
    code: '0',
    msg: 'Successfully called test API'
    )


    )



    /* Only for production i.e: - All others are to be handled by Angular's router /
    app.get('/
    ', (req, res) =>
    res.sendFile(path.join(__dirname + '/../dist/index.html'));
    );



    http.listen(3001, function()
    console.log(App started on port 3001)
    )


When I then run from node server/server.js, when attempting to access localhost:3001, I get the following error:




Uncaught Error: Unexpected value 't' imported by the module 't'.
Please add a @NgModule annotation.




How can I get my project to run successfully in express?




[ad_2]

لینک منبع