Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Express to load static files

// For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public:
app.use(express.static('public'))

// To use multiple static assets directories, call the express.static middleware function multiple times:
app.use(express.static('public'))
app.use(express.static('files'))

// To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:

app.use('/static', express.static('public'))

// However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve:

const path = require('path')
app.use('/static', express.static(path.join(__dirname, 'public')))
Comment

express serve static files

app.use('/', express.static('public'));
Comment

express js static files

app.use(express.static('public'))
Comment

serve static files from express

const path = require('path')
app.use('/static', express.static(path.join(__dirname, 'public')))
Comment

Serving static files in Express

var express = require('express');
var app = express();
var path = require('path');

// For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public:
app.use(express.static('public'))

// To use multiple static assets directories, call the express.static middleware function multiple times:
app.use(express.static('public'))
app.use(express.static('files'))

// To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:

app.use('/static', express.static('public'))

// However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve:

const path = require('path')
app.use('/static', express.static(path.join(__dirname, 'public')))
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript document load 
Javascript :: console.log ejs 
Javascript :: javascript does key exist 
Javascript :: How to tell if an attribute exists on an object 
Javascript :: remove a class from all elements javascript 
Javascript :: detecting screen width in jquery 
Javascript :: iframe getelementbyid 
Javascript :: moment today date 
Javascript :: node wait 10 seconds 
Javascript :: get value from url 
Javascript :: js this binding setinterval 
Javascript :: stop immediate propagation 
Javascript :: _redirects in netlify 
Javascript :: how to install font-awesome 
Javascript :: jquery disable select 
Javascript :: if media query jquery 
Javascript :: moment add 30 days 
Javascript :: generate jwt secret key 
Javascript :: js tolocalestring without seconds 
Javascript :: js stop form submit 
Javascript :: display pm or am on date js 
Javascript :: on change jquery 
Javascript :: view engine setup express 
Javascript :: nodejs make directory 
Javascript :: step over vs step into vs step out 
Javascript :: check undefined object javascript one liner set to emtpy 
Javascript :: check if is array js 
Javascript :: adding event on keypress in javascript 
Javascript :: redirect javascript timer 
Javascript :: javascript change page title 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =