0

enter image description hereI am learning to build a simple mean stack application. I have default angular project which runs fine. I want to run the angular app through express server but it showing error "Cannot GET /" in the browser. Here is my code:

    const express = require("express");
const compression = require("compression");

const _port = 4100;
const _app_folder = 'src/dist/app';

const app = express();
app.use(compression());


// ---- SERVE STATIC FILES ---- //
app.get('*.*', express.static(_app_folder, {maxAge: '1y'}));

// ---- SERVE APLICATION PATHS ---- //
app.all('*', function (req, res) {
    res.status(200).sendFile('/', {root: _app_folder});
});

// ---- START UP THE NODE SERVER  ----
app.listen(_port, function () {
    console.log("Node Express server for " + app.name + " listening on http://localhost:" + _port);
});
  • If you want to use nodejs with expressjs. I will suggest you to use express-generator. https://expressjs.com/en/starter/generator.html – Puneet Sharma Dec 16 '19 at 16:46
  • is there any reason to not run frontend independently? – Siddharth Pal Dec 16 '19 at 16:48
  • You have to use the router with express for using express. `var router = express.Router();` – Puneet Sharma Dec 16 '19 at 16:49
  • @puneetSharm can you kindly elaborate it more by giving code example. – Usama Hameed Dec 16 '19 at 16:51
  • you can run frontend independently and use the backend IP with port to request. you also have to give the CORS permission in nodejs application. Check this link https://stackoverflow.com/a/57821053/4452091 – Puneet Sharma Dec 16 '19 at 16:53
  • Are you sure this is correct: src/dist/app ? – MikeOne Dec 16 '19 at 16:54
  • @MikeOne what exactly is required in this path, in src/dist/app i have a default files of Angular like app.component.css, app.component.html, routing file and server.js file in which above code is written. – Usama Hameed Dec 16 '19 at 16:58
  • You can following step provided in this link for setup frontend and Backend independently. https://levelup.gitconnected.com/simple-application-with-angular-6-node-js-express-2873304fff0f – Puneet Sharma Dec 16 '19 at 17:00
  • Is looks strange that you have a dist folder inside your src folder.. – MikeOne Dec 16 '19 at 17:22

0 Answers0