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

how express serve public folder

// projectDirectory/src/index.js
const path = require('path')
const publicDirectoryPath = path.join(__dirname, '../public')
app.use(express.static(publicDirectoryPath))

// projectDirectory/public -> create index.html
// localhost:3000/index.html -> Here you go..

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.static public

app.use('/public', express.static(path.join(__dirname, '../public')))
                                  // use path here              
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 :: bootstrap dropdown not working in angular 8 
Javascript :: how to take input from user nodejs 
Javascript :: has not class jquery 
Javascript :: firebase realtime database delete child 
Javascript :: Error: Error: Could not resolve [object Object] / undefined at Scope.resolve 
Javascript :: find last element with class jquery 
Javascript :: add icon to text input react native 
Javascript :: debounce in react native hooks 
Javascript :: settimeout function 
Javascript :: react input number 
Javascript :: js simulate keyboard input 
Javascript :: format number to 2 digits javascript 
Javascript :: React Native - navigation is undefined 
Javascript :: print webpage in javascript 
Javascript :: How to download files using axios 
Javascript :: bash parse json 
Javascript :: angular serve on different port 
Javascript :: sending form data with fetch using js 
Javascript :: check jquery version on console 
Javascript :: javascript is variable number or string 
Javascript :: find an object in an array of objects javascript 
Javascript :: jest check array of string 
Javascript :: js check query string 
Javascript :: remove whitespace javascript 
Javascript :: how to loop through an object using lodash 
Javascript :: regular expression javascript for phone number 
Javascript :: filter special characters javascript 
Javascript :: how to get os information nodejs 
Javascript :: combine two arrays javascript 
Javascript :: gms2 object method 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =