Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node express server static files

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

//app.use(express.static(__dirname)); // Current directory is root
app.use(express.static(path.join(__dirname, 'public'))); //  "public" off of current is root

app.listen(80);
console.log('Listening on port 80');
Comment

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

static folder express

// dependencies
const path = require('path');

// set static folder
app.set(express.static(path.join(__dirname, 'public')));
Comment

how to send static file in express

app.use('/', express.static(__dirname));
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

public static expressjs

//please read the expressjs docs
//  https://expressjs.com/en/starter/static-files.html
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 cors error 
Javascript :: how to add up all the numbers in between 0 and that number 
Javascript :: onselect javascript 
Javascript :: select text with javascript 
Javascript :: angular json to file and download 
Javascript :: react native get uri of the image in the app assets folder 
Javascript :: js math.trunc 
Javascript :: browserslisterror contains both .browserslistrc and package.json with browsers 
Javascript :: js add html element to div 
Javascript :: boolean constructor js 
Javascript :: javascript count time 
Javascript :: jquery insert after element 
Javascript :: javascript scroll to top 
Javascript :: link react router dom 
Javascript :: How to access return value of promise 
Javascript :: jsonarray add jsonobject 
Javascript :: protected route in react js 
Javascript :: Close popup window 
Javascript :: javascript if browser out of focus 
Javascript :: js create json array 
Javascript :: datatables search not working 
Javascript :: javascript new date dd/mm/yyyy 
Javascript :: angular ngfor counter 
Javascript :: remove duplicated from array of ojects 
Javascript :: window.innerHeight react js 
Javascript :: include other js files in a js file 
Javascript :: javascript json append array 
Javascript :: javascript password hashing 
Javascript :: js set datetime 
Javascript :: javascript is radio button checked 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =