Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Node.js Tutorial

Node HOME Node Intro Node Get Started Node JS Requirements Node.js vs Browser Node Cmd Line Node V8 Engine Node Architecture Node Event Loop

Asynchronous

Node Async Node Promises Node Async/Await Node Errors Handling

Module Basics

Node Modules Node ES Modules Node NPM Node package.json Node NPM Scripts Node Manage Dep Node Publish Packages

Core Modules

HTTP Module HTTPS Module File System (fs) Path Module OS Module URL Module Events Module Stream Module Buffer Module Crypto Module Timers Module DNS Module Assert Module Util Module Readline Module

JS & TS Features

Node ES6+ Node Process Node TypeScript Node Adv. TypeScript Node Lint & Formatting

Building Applications

Node Frameworks Express.js Middleware Concept REST API Design API Authentication Node.js with Frontend

Database Integration

MySQL Get Started MySQL Create Database MySQL Create Table MySQL Insert Into MySQL Select From MySQL Where MySQL Order By MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL Join
MongoDB Get Started MongoDB Create DB MongoDB Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit MongoDB Join

Advanced Communication

GraphQL Socket.IO WebSockets

Testing & Debugging

Node Adv. Debugging Node Testing Apps Node Test Frameworks Node Test Runner

Node.js Deployment

Node Env Variables Node Dev vs Prod Node CI/CD Node Security Node Deployment

Perfomance & Scaling

Node Logging Node Monitoring Node Performance Child Process Module Cluster Module Worker Threads

Node.js Advanced

Microservices Node WebAssembly HTTP2 Module Perf_hooks Module VM Module TLS/SSL Module Net Module Zlib Module Real-World Examples

Hardware & IoT

RasPi Get Started RasPi GPIO Introduction RasPi Blinking LED RasPi LED & Pushbutton RasPi Flowing LEDs RasPi WebSocket RasPi RGB LED WebSocket RasPi Components

Node.js Reference

Built-in Modules EventEmitter (events) Worker (cluster) Cipher (crypto) Decipher (crypto) DiffieHellman (crypto) ECDH (crypto) Hash (crypto) Hmac (crypto) Sign (crypto) Verify (crypto) Socket (dgram, net, tls) ReadStream (fs, stream) WriteStream (fs, stream) Server (http, https, net, tls) Agent (http, https) Request (http) Response (http) Message (http) Interface (readline)

Resources & Tools

Node.js Compiler Node.js Server Node.js Quiz Node.js Exercises Node.js Syllabus Node.js Study Plan Node.js Certificate

Node.js with Frontend Frameworks


Introduction to Frontend Integration with Node.js

Node.js provides a backend foundation that integrates with modern JavaScript frontend frameworks, enabling developers to build full-stack applications within just the JavaScript ecosystem.

This approach offers several advantages:

  • Unified Language: Use JavaScript/TypeScript across the entire stack
  • Code Sharing: Share validation, types, and utilities between frontend and backend
  • Developer Experience: Consistent tooling and package management with npm/yarn
  • Performance: Efficient data transfer with JSON and modern protocols
  • Ecosystem: Access to a vast collection of packages for both frontend and backend

Common Integration Patterns

1. API-First Architecture

Node.js backend exposes RESTful or GraphQL APIs consumed by a separate frontend application.

// Example API endpoint
app.get('/api/products', (req, res) => {
  res.json([{ id: 1, name: 'Product' }]);
});

2. Server-Side Rendering (SSR)

Node.js renders initial page on the server for better SEO and performance.

// Next.js page
export async function getServerSideProps() {
  const res = await fetch('https://api.example.com/data');
  return { props: { data: await res.json() } };
}

3. Micro-Frontends

Multiple frontend applications integrated into a unified experience.

// Module Federation in webpack.config.js
new ModuleFederationPlugin({
  name: 'app1',
  filename: 'remoteEntry.js',
  exposes: { './Component': './src/Component' }
})

Node.js with React

React is a declarative, efficient, and flexible JavaScript library for building user interfaces.

It enables developers to create reusable UI components and efficiently update and render them when data changes.

Why Use React with Node.js?

  • Component-Based Architecture: Build encapsulated components that manage their own state
  • Virtual DOM: Efficient updates and rendering
  • Rich Ecosystem: Large community and extensive package ecosystem
  • Developer Tools: Excellent debugging and development tools

Setting Up a React App with Node.js Backend

1. Create React App (Frontend)

npx create-react-app my-app
cd my-app
npm start

2. Set Up Node.js Backend

mkdir backend
cd backend
npm init -y
npm install express cors

Example: Node.js API with React Frontend

// Node.js backend (Express)
const express = require('express');
const cors = require('cors');
const app = express();

// Enable CORS for React frontend
app.use(cors());

app.get('/api/data', (req, res) => {
  res.json({ message: 'Hello from Node!' });
});

app.listen(8080, () => {
  console.log('Server running on port 8080');
});

// React frontend component
import { useState, useEffect } from 'react';

function App() {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch('http://localhost:8080/api/data')
      .then(res => res.json())
      .then(data => {
        setData(data);
        setLoading(false);
      });
  }, []);

  return (
    <div>
      {loading ? 'Loading...' : data.message}
    </div>
  );
}

Node.js with Angular

Angular is a comprehensive platform and framework for building scalable single-page applications using TypeScript.

It provides a complete solution with built-in features for routing, forms, HTTP client, and more, making it a robust choice for enterprise applications.

Key Features of Angular with Node.js

  • TypeScript Support: Built with TypeScript for better tooling and type safety
  • Dependency Injection: Built-in DI system for better component organization
  • Modular Architecture: Organized into modules, components, and services
  • RxJS Integration: Powerful reactive programming with Observables
  • Angular CLI: Command-line interface for project generation and build tools

Setting Up Angular with Node.js Backend

1. Install Angular CLI

npm install -g @angular/cli

2. Create New Angular Project

ng new angular-nodejs-app
cd angular-nodejs-app

Tip: Use --routing flag to include routing and --style=scss for SCSS styling when creating a new project.

Example: Node.js API with Angular Frontend

// Node.js backend (Express)
const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/api/users', (req, res) => {
  res.json([
    { id: 1, name: 'John Doe' },
    { id: 2, name: 'Jane Smith' }
  ]);
});

app.listen(8080, () => {
  console.log('Server running on port 8080');
});

// Angular service (user.service.ts)
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

interface User {
  id: number;
  name: string;
}

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private apiUrl = 'http://localhost:8080/api/users';

  constructor(private http: HttpClient) { }

  getUsers(): Observable<User[]> {
    return this.http.get<User[]>(this.apiUrl);
  }
}

Node.js with Vue.js

Vue.js is a progressive, approachable, and performant JavaScript framework for building user interfaces.

It provides a gentle learning curve and flexible architecture, making it an excellent choice for both small projects and large-scale applications when combined with Node.js backends.

Why Choose Vue.js with Node.js?

  • Progressive Framework: Scales from a library to a full-featured framework
  • Reactive Data Binding: Simple and intuitive two-way data binding
  • Component-Based: Build encapsulated, reusable components
  • Vue CLI: Powerful command-line interface for project scaffolding
  • Vuex: Centralized state management for complex applications

Setting Up Vue.js with Node.js Backend

1. Install Vue CLI

npm install -g @vue/cli

2. Create New Vue Project

vue create vue-nodejs-app
cd vue-nodejs-app

Tip: Choose "Manually select features" during project creation to include Vuex, Router, and other essential features.

Example: Node.js API with Vue.js Frontend

// Node.js backend (Express)
const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/api/products', (req, res) => {
  res.json([
    { id: 1, name: 'Product A', price: 29.99 },
    { id: 2, name: 'Product B', price: 49.99 }
  ]);
});

app.listen(8080, () => {
  console.log('Server running on port 8080');
});

// Vue.js component
<template>
  <div>
    <h2>Products</h2>
    <div v-if="loading">Loading...</div>
    <ul v-else>
      <li v-for="product in products" :key="product.id">
        {{ product.name }} - ${{ product.price }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [],
      loading: true
    };
  },
  created() {
    fetch('http://localhost:8080/api/products')
      .then(response => response.json())
      .then(data => {
        this.products = data;
        this.loading = false;
      });
  }
};
</script>

Node.js with Svelte

Svelte is a revolutionary approach to building user interfaces that compiles your code to highly efficient vanilla JavaScript at build time, rather than interpreting your application code at runtime.

This results in smaller bundle sizes and better performance compared to traditional frameworks.

Why Choose Svelte with Node.js?

  • No Virtual DOM: Compiles to vanilla JavaScript for better performance
  • Smaller Bundle Size: No framework runtime to ship to the browser
  • Simpler Code: Less boilerplate than traditional frameworks
  • Reactive by Default: Automatic updates without complex state management
  • Scoped CSS: Component-scoped styles without CSS-in-JS

Setting Up Svelte with Node.js Backend

1. Create a New Svelte Project

npx degit sveltejs/template svelte-nodejs-app
cd svelte-nodejs-app
npm install

2. Set Up Development Server

npm install -D @sveltejs/adapter-node
npm run dev

Tip: Use npm run build to create a production build that can be served by your Node.js backend.

Example: Node.js API with Svelte Frontend

// Node.js backend (Express)
const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/api/todos', (req, res) => {
  res.json([
    { id: 1, text: 'Learn Node.js', done: true },
    { id: 2, text: 'Learn Svelte', done: false },
    { id: 3, text: 'Build an app', done: false }
  ]);
});

app.listen(8080, () => {
  console.log('Server running on port 8080');
});

<script>
  import { onMount } from 'svelte';

  let todos = [];
  let loading = true;

  onMount(async () => {
    const response = await fetch('http://localhost:8080/api/todos');
    todos = await response.json();
    loading = false;
  });

  function toggleTodo(id) {
    todos = todos.map(todo => {
      if (todo.id === id) {
        return { ...todo, done: !todo.done };
      }
      return todo;
    });
  }
</script>

<h2>Todo List</h2>
{#if loading}
  <p>Loading...</p>
{:else}
  <ul>
    {#each todos as todo (todo.id)}
      <li>
        </li>
          type="checkbox"
          checked={todo.done}
          on:change={() => toggleTodo(todo.id)}
        />
        <span class={todo.done ? 'done' : ''}>{todo.text}</span>
    </li>
    {/each}
  </ul>
{/if}

<style>
  .done {
    text-decoration: line-through;
    color: #888;
  }

Best Practices for Node.js with Frontend Frameworks

1. Project Structure & Organization

Monorepo vs Polyrepo

  • Monorepo: Single repository for both frontend and backend
  • Polyrepo: Separate repositories with clear API contracts

Recommended Structure

project/
├── backend/ # Node.js backend
│ ├── src/
│ ├── package.json
│ └── ...
└── frontend/ # Frontend framework
├── src/
├── package.json
└── ...

2. API Design & Communication

RESTful API Best Practices

  • Use proper HTTP methods (GET, POST, PUT, DELETE)
  • Return appropriate status codes
  • Implement consistent response formats
  • Version your API (e.g., /api/v1/...)

Real-time Communication

// Server-side with Socket.io
io.on('connection', (socket) => {
  socket.emit('message', 'Welcome!');
  socket.on('chatMessage', (msg) => {
    io.emit('message', msg);
  });
});

3. Security Best Practices

Essential Security Middleware

// Install required packages
npm install helmet cors express-rate-limit
  express-mongo-sanitize xss-clean hpp
// Basic security setup
app.use(helmet());
app.use(cors({ origin: process.env.FRONTEND_URL }));
app.use(express.json({ limit: '10kb' }));
app.use(mongoSanitize());
app.use(xss());

4. Performance Optimization

Frontend

  • Code splitting and lazy loading
  • Image optimization
  • Bundle analysis (webpack-bundle-analyzer)
  • Service workers for offline support

Backend

  • Implement caching (Redis, Memcached)
  • Database indexing and query optimization
  • Connection pooling
  • Compression middleware

5. Development & Deployment

Environment Configuration

// .env.example
NODE_ENV=development
PORT=3000
MONGODB_URI=your_mongodb_uri
JWT_SECRET=your_jwt_secret
FRONTEND_URL=http://localhost:3000

CI/CD Pipeline

  • Automated testing (Jest, Cypress)
  • Docker for containerization
  • Blue-green deployments
  • Monitoring and logging



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.