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

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

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 :: ajax header 
Javascript :: get window size javascript 
Javascript :: disable input field from jquery 
Javascript :: js root url 
Javascript :: javascript insert sibling after 
Javascript :: cypress set window size 
Javascript :: jquery checkbox checked value 
Javascript :: hack google dinosaur 
Javascript :: javascrip reverse text 
Javascript :: node js procfile heroku starter 
Javascript :: add background image to div using jquery 
Javascript :: select2 on change 
Javascript :: redirect in netlify react 
Javascript :: use icon in node js html 
Javascript :: html string to html 
Javascript :: javascript link to go back 
Javascript :: sequelize exclude attribute 
Javascript :: get age using moment 
Javascript :: javascript filter array by another array 
Javascript :: history.push in nextjs 
Javascript :: write in utf8 fs 
Javascript :: email regex specific domain simple 
Javascript :: chart js no points 
Javascript :: getelementbyclass 
Javascript :: bar chart height and with change in chart.js 
Javascript :: js yesterday date 
Javascript :: if is array javascript 
Javascript :: adding event on enter key keypress in javascript 
Javascript :: get data in date filter sequelize 
Javascript :: expressjs create encrypted password 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =