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.