- Spec files
npm-express
Describes: pkg:npm/express@4.21.x
- Description
- Fast, unopinionated, minimalist web framework for Node.js
- Author
- tessl
- Last updated
application.md docs/
1# Application Management23Core Express application creation, configuration, and server management functionality for setting up web applications and APIs.45## Capabilities67### Application Factory89Creates a new Express application instance with routing and middleware capabilities.1011```javascript { .api }12/**13* Creates an Express application instance14* @returns {Application} Express application instance with routing and middleware15*/16function express(): Application;17```1819**Usage Example:**2021```javascript22const express = require('express');23const app = express();2425// App is now ready for configuration and routing26app.get('/', (req, res) => {27res.send('Hello World!');28});29```3031### Application Settings3233Configure application behavior through settings that control Express features.3435```javascript { .api }36/**37* Set application setting to a value38* @param {string} setting - Setting name39* @param {any} value - Setting value40* @returns {Application} Application instance for chaining41*/42app.set(setting: string, value: any): Application;4344/**45* Get application setting value46* @param {string} setting - Setting name47* @returns {any} Setting value48*/49app.get(setting: string): any;5051/**52* Enable a boolean setting53* @param {string} setting - Setting name54* @returns {Application} Application instance for chaining55*/56app.enable(setting: string): Application;5758/**59* Disable a boolean setting60* @param {string} setting - Setting name61* @returns {Application} Application instance for chaining62*/63app.disable(setting: string): Application;6465/**66* Check if a boolean setting is enabled67* @param {string} setting - Setting name68* @returns {boolean} True if enabled, false otherwise69*/70app.enabled(setting: string): boolean;7172/**73* Check if a boolean setting is disabled74* @param {string} setting - Setting name75* @returns {boolean} True if disabled, false otherwise76*/77app.disabled(setting: string): boolean;78```7980**Common Settings:**8182- `case sensitive routing` - Enable case-sensitive routes (default: false)83- `env` - Environment mode (default: process.env.NODE_ENV || 'development')84- `etag` - ETag generation setting (default: 'weak')85- `jsonp callback name` - JSONP callback parameter name (default: 'callback')86- `query parser` - Query string parser setting (default: 'extended')87- `strict routing` - Enable strict routing (default: false)88- `subdomain offset` - Subdomain offset for parsing (default: 2)89- `trust proxy` - Proxy trust configuration (default: false)90- `view cache` - Template cache setting (default: true in production)91- `view engine` - Default template engine92- `views` - Views directory path (default: './views')93- `x-powered-by` - X-Powered-By header setting (default: true)9495**Usage Examples:**9697```javascript98const app = express();99100// Configure settings101app.set('view engine', 'ejs');102app.set('views', './templates');103app.set('port', process.env.PORT || 3000);104105// Enable/disable features106app.enable('trust proxy');107app.disable('x-powered-by');108109// Check settings110if (app.enabled('trust proxy')) {111console.log('Proxy trust is enabled');112}113114const port = app.get('port');115console.log(`Server will run on port ${port}`);116```117118### Application Path119120Get the canonical path of the application when mounted as a sub-application.121122```javascript { .api }123/**124* Returns the canonical path of the application125* @returns {string} The path where the app is mounted, empty string for main app126*/127app.path(): string;128```129130**Usage Example:**131132```javascript133const express = require('express');134const app = express();135const admin = express();136137// Mount admin as sub-application138app.use('/admin', admin);139140console.log(app.path()); // '' (empty string for main app)141console.log(admin.path()); // '/admin'142143// With nested mounting144const users = express();145admin.use('/users', users);146console.log(users.path()); // '/admin/users'147```148149### Server Startup150151Start HTTP server to listen for incoming connections.152153```javascript { .api }154/**155* Start HTTP server listening on specified port and hostname156* @param {number} [port] - Port number (default: 0 for random port)157* @param {string} [hostname] - Hostname (default: undefined, listens on all interfaces)158* @param {Function} [callback] - Callback function called when server starts159* @returns {http.Server} Node.js HTTP server instance160*/161app.listen(port?: number, hostname?: string, callback?: () => void): http.Server;162163/**164* Start HTTP server listening on specified port165* @param {number} [port] - Port number166* @param {Function} [callback] - Callback function167* @returns {http.Server} Node.js HTTP server instance168*/169app.listen(port?: number, callback?: () => void): http.Server;170171/**172* Start HTTP server with callback only173* @param {Function} [callback] - Callback function174* @returns {http.Server} Node.js HTTP server instance175*/176app.listen(callback?: () => void): http.Server;177```178179**Usage Examples:**180181```javascript182const app = express();183184// Basic server startup185app.listen(3000, () => {186console.log('Server running on port 3000');187});188189// Specify hostname190app.listen(3000, 'localhost', () => {191console.log('Server running on localhost:3000');192});193194// Use environment port195const port = process.env.PORT || 3000;196app.listen(port, () => {197console.log(`Server running on port ${port}`);198});199200// Get server instance for further configuration201const server = app.listen(3000);202server.on('error', (err) => {203console.error('Server error:', err);204});205```206207### Template Engine Integration208209Register and configure template engines for rendering dynamic content.210211```javascript { .api }212/**213* Register template engine for specific file extension214* @param {string} ext - File extension (without dot)215* @param {Function} callback - Template engine function216* @returns {Application} Application instance for chaining217*/218app.engine(ext: string, callback: TemplateEngine): Application;219220/**221* Render view template with optional locals222* @param {string} view - View name/path223* @param {Object} [options] - Template locals and options224* @param {Function} [callback] - Callback function with rendered HTML225*/226app.render(view: string, options?: any, callback?: (err: Error | null, html?: string) => void): void;227228/**229* Template engine function signature230*/231type TemplateEngine = (filePath: string, options: any, callback: (err: Error | null, rendered?: string) => void) => void;232```233234**Usage Examples:**235236```javascript237const app = express();238239// Set view engine240app.set('view engine', 'ejs');241app.set('views', './views');242243// Custom template engine244app.engine('ntl', (filePath, options, callback) => {245// Custom template processing246fs.readFile(filePath, (err, content) => {247if (err) return callback(err);248249const rendered = content.toString()250.replace(/\{\{(.+?)\}\}/g, (match, key) => options[key] || '');251252callback(null, rendered);253});254});255256// Render template programmatically257app.render('index', { title: 'My App' }, (err, html) => {258if (err) {259console.error('Render error:', err);260} else {261console.log('Rendered HTML:', html);262}263});264```265266## Application Instance Properties267268```javascript { .api }269/**270* Express application instance interface271*/272interface Application {273/** Application locals available to all templates */274locals: { [key: string]: any };275/** Application settings */276settings: { [key: string]: any };277/** Application mount path when used as sub-app */278mountpath: string | string[];279}280```281282**Usage Examples:**283284```javascript285const app = express();286287// Set application locals288app.locals.title = 'My Express App';289app.locals.email = 'admin@example.com';290291// Access in templates (EJS example)292// <h1><%= title %></h1>293// <p>Contact: <%= email %></p>294295// Check mount path (useful for sub-applications)296console.log(app.mountpath); // '/' for main app297```