What are the steps for integrating angular2 starter with express?

Can someone explain how to integrate the following Angular starter template into an Express framework?

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

I am able to start the webpack dev server without any issues, but I would like to utilize additional libraries such as cookieparser (https://www.npmjs.com/package/cookie-parser).

When using npm start with webpack-dev-server, I am unsure of which file to modify in order to include app.use statements and import the cookieparser library.

Answer №1

If you're currently using webpack.dev.js in the latest version, make sure to include the following changes:

node: {
  global: true,
  crypto: 'empty',
  process: true,
  module: false,
  clearImmediate: false,
  setImmediate: false,
  fs: 'empty',
  net: 'empty'
}

Once you've updated with these configurations, everything should function as intended.

Answer №2

Just wanted to share my approach with you - I have an express server set up to communicate with a separate front-end angular server. By keeping them separate, I avoid adding redundant libraries to either one. When I'm ready, I simply set the build path for 'dist' to point to my server folder like this:

"apps": [
    {
      "root": "src",
      "outDir": "server/dist", //<--- Here, I specify the location of my server.js, making it easy to build and prepare for production
      "assets": [
        "assets",
        "favicon.ico"
      ],

Here is my complete server.js file that I run with 'node server.js':

let express = require('express');
let passport = require('passport');
let session = require('express-session');
const config  = require('./server/auth/config');
let oauth2orize = require('oauth2orize');
const app = express();
const cors = require('cors');
const html = __dirname + '/dist';

app.use(passport.initialize());
app.use(passport.session());

app.use(cors());
app.options('*', cors());
app.use('/api', require('./server/misc/routemanager'));

/**
 * This line tells the server to serve our angular site (after running 'ng build' with it)
 */
app.use(express.static(html));
app.get('*', function(req, res) {
    res.sendFile(html + '/index.html')
});

app.listen(4949, () => {
    console.log(`Node Express server listening on http://localhost:4949`);
});

Now I have the freedom to build my express backend as needed, communicate with it using routes, and still launch everything with a single node command.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you please explain how I can retrieve information from a Firebase collection using the NextJs API, Firebase Firestore, axios, and TypeScript?

I'm currently utilizing api routes within NextJS 13 to retrieve data from Firebase. The code for this can be found in api/locations.tsx: import { db } from "../../firebase"; import { collection, getDocs } from "firebase/firestore"; ...

"An 'undefined' message appeared on the server console following the completion of the

I developed a basic HTML application using Node.js. Below is the code snippet from server.ts: import express = require('express'); import http = require('http'); import path = require('path'); import cookieParser = require(& ...

Integrate a JS file into my Angular 4 project

For one of my components, I am looking to implement a specific effect: https://codepen.io/linrock/pen/Amdhr Initially, I attempted to convert the JavaScript code to TypeScript, but faced challenges. Eventually, I decided to directly copy the JS file from ...

Tips for generating a TypeScript config file tailored to your application:

Currently, I am developing a web application with TypeScript and I have the need to establish a straightforward configuration file to define specific aspects like color schemes. What is the most effective approach for creating this configuration file in T ...

Every time I attempt to serve Firebase functions, it alerts me that the port is closed

While attempting to run my firebase function, I encountered a persistent error message that reads: Error: Port {{Port Here}} is not open, could not start functions emulator.. Despite trying different ports, I have been unable to resolve this issue. I' ...

Challenge with uploading Minio presigned URLs

I am encountering a problem with the Minio presigned URL. While I have successfully obtained the URL and used the PUT method to insert my file into my Minio bucket, I am unable to open certain file types such as jpg, png, or pdf. This is due to Minio autom ...

What are the reasons behind the restriction on using non-public members in TypeScript classes?

Consider the following scenario: class Trait { publicMethod() { this.privateMethod(); // do something more } private privateMethod() { // perform a useful action } } When attempting to implement it like this: cla ...

User interface designed for objects containing multiple keys of the same data type along with a distinct key

I have a question that relates to this topic: TypeScript: How to create an interface for an object with many keys of the same type and values of the same type?. My goal is to define an interface for an object that can have multiple optional keys, all of t ...

What is the best way to integrate the express-session logic with Prisma for optimal performance?

Hi there, I recently started using Prisma and want to integrate it with PostgreSQL. My main goal is to implement authentication in my backend, but I encountered issues while trying to create a session table. When working with raw SQL, I managed to add the ...

Consistent tree view nesting persists with each refresh

I have recently transitioned to Angular (7) from a C# background. Currently, I am using asp.net core 2.2 along with the default template that comes with a new Angular project (home, counter, fetch-data). The tree view is bound and retrieved from a controll ...

Leveraging async/await within a model function and then integrating it into a controller function within an Express MVC architecture with node-pg

I am currently working on getting a model method (specifically dealing with 'Read' from CRUD) to function correctly with a controller in the node app that I am constructing. The goal is to render a list of cars retrieved from a Postgres database ...

Breaking down code with Webpack for future extensibility

We are in the process of developing a game and have successfully implemented code-splitting to separate vendor libraries and the core engine into individual bundles, as well as splitting levels into separate bundles. As we plan for future releases where t ...

Error: The file named '/accounts.ts' cannot be recognized as a module within a Node.js API

After researching this issue, I have found some answers but none of them seem to solve my problem. Below is the code in my model file: // accounts.ts const mongoose = require('mongoose'); var autoincrement = require('simple-mongoose-autoi ...

Obtain the value of a template variable in Angular 2

I am seeking information on how to access the values of selected items in templates. Specifically, I want to understand how to retrieve the selected value of IPMIDisplayTime and IPMIDisplayTime within the template for later use. import {ViewChild, Elem ...

TypeScript issue encountered with parseInt() function when used with a numeric value

The functionality of the JavaScript function parseInt allows for the coercion of a specified parameter into an integer, regardless of whether that parameter is originally a string, float number, or another type. While in JavaScript, performing parseInt(1. ...

Express middleware failing to execute

According to the Express documentation, a middleware should run each time the app is launched using this code snippet: var app = express(); app.use(function (req, res, next) { console.log('Time:', Date.now()); next(); }); However, in a ...

I am encountering an error in TypeScript stating that a property does not exist on the Response type

Hey there, I've been working on a custom advanced results page that allows for various queries like /articles?name="". This is my first time using TypeScript and while migrating my code over, I encountered a TypeScript error at the very end. // esli ...

The user interface in Angular 7 does not reflect the updated values after subscribing

Upon examining the code provided, it is evident that the UI does not reflect the updated value even though the field is set correctly. I have attempted two different approaches but have not explored the change detection approach as I believe the current c ...

Issue encountered with Node.js port authorization on Windows 10

Encountering an issue when attempting to launch a server in Express.js resulting in the following error: [nodemon] 2.0.12 [nodemon] at any time, enter `rs` to restart. [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] sta ...

How to Send Error Codes with res.render in Node.js using Express

Currently developing with nodejs and express, I am seeking a way to display a customized 404 error page. While I have successfully implemented the error page, I am still searching for a method to include an error code when using res.render(). Previous so ...