This is a template for server-side rendering (SSR) that combines Express.js with Handlebars templates and React for interactive client-side components. The project uses Webpack to bundle React, SASS for styling, and express-session for session-based authentication.
- Server-Side Rendering (SSR): Pages are rendered on the server using Handlebars templates for faster first paint.
- React Integration: React components are loaded and optionally hydrated in the browser from Handlebars views.
- Webpack Bundling: React entry points and dynamic imports are bundled with Webpack 5.
- SASS Styling: Modular SCSS files compile into a single CSS output.
- Authentication & Sessions: Session-based authentication with protected routes and login/logout flow.
- Development Tools: Watch mode for JavaScript, CSS, and server changes in development.
- Modular Architecture: Clean separation of routes, views, components, middleware, and utilities.
- Backend: Node.js, Express.js
- Templating: Handlebars (express-handlebars)
- Frontend: React 19, React DOM
- Build Tools: Webpack 5, Babel
- Styling: SASS/SCSS
- Authentication:
express-session,session-file-store - Development:
concurrently, Node.js watch mode
├── src/
│ ├── config/ # Config helpers and session configuration
│ │ ├── handlebarsHelpers.js
│ │ └── sessionConfig.js
│ ├── middlewares/ # Express middleware
│ │ └── auth.js
│ ├── react/ # React entrypoint, component map, and components
│ │ ├── componentMap.js
│ │ ├── index.jsx
│ │ └── components/
│ │ ├── App.jsx
│ │ ├── NavDropdown.jsx
│ │ └── Test2.jsx
│ ├── routes/ # Express routes
│ │ ├── api.routes.js
│ │ ├── auth.routes.js
│ │ └── views.routes.js
│ ├── sass/ # SCSS stylesheets
│ │ ├── index.scss
│ │ └── [partials].scss
│ ├── util/ # Utility functions
│ │ ├── dirname.js
│ │ └── eraseSessions.js
│ └── views/ # Handlebars templates
│ ├── index.handlebars
│ ├── login.handlebars
│ ├── protected.handlebars
│ ├── test1.handlebars
│ ├── test2.handlebars
│ ├── layouts/
│ └── partials/
├── localData/ # Local data storage
│ └── sessions/ # Session files
├── public/ # Static assets output
│ ├── assets/
│ ├── css/
│ └── js/
├── babel.config.js
├── webpack.config.js
├── package.json
└── index.js
- Node.js (v16 or higher)
- SASS (v1.9 or higher)
- npm or yarn
- Clone or download this repository
- Navigate to the project directory
- Install dependencies:
npm installCreate a .env file in the root directory with the following variables:
PORT=10100
SECRET_SESSION=your-secret-key-here
To start the development server with hot reloading:
npm run devThis command runs:
- Webpack in watch mode for React bundles
- Node.js server in watch mode
- SASS compiler in watch mode
The application will be available at http://localhost:10100
To build for production all is seted as prestart script, but you can also run:
npm run build && npm run build:cssThis compiles and minifies all assets.
To start the production server:
npm startThe template includes a basic login flow with session management:
- Login: POST to
/auth/loginwithemailandpasswordfields - Logout: POST to
/auth/logout - Protected Routes: Use
requireAuthmiddleware to protect routes - Session State: Available in templates via
isAuthenticatedandsessionUserlocals
Default test credentials: test@example.com / asdasd
- Create a React component under
src/react/components/. - Register it in
src/react/componentMap.js. - Include a matching
<div id="your-component"></div>in a Handlebars template.
Example mapping:
{
id: 'your-component',
component: () => import(/* webpackChunkName: "your-component" */ './components/YourComponent.jsx'),
hydrate: true,
pages: ['/'],
props: {},
}The runtime loads only the components mapped for the current page and hydrates them when hydrate: true.
npm run dev- Start development server with hot reloadingnpm run build- Build production assetsnpm run build:watch- Build assets in watch modenpm run start- Start production servernpm run dev:css- Watch and compile SASS in development modenpm run build:css- Compile and compress SASS for production
- Fix the spacing rendering issue on handlebars to allow easily hydratation
- Implement database integration for user management
- Add more authentication features (registration, password reset)
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or improvements.
This project is licensed under the MIT License.