Home Web Front-end JS Tutorial How to host static files with Express

How to host static files with Express

Jun 04, 2018 am 09:45 AM
express hosting static

This time I will show you how to use Express to host static files, and what are the precautions for using Express to host static files. The following is a practical case, let's take a look.

Middlewareexpress.static

When we use express to initialize a directory, it will be in app.js I saw a lot of recommended app.use.

One of the main middlewares is express.static (middleware still retained in version 4.0)

var express = require('express');
var app = express();
app.use('/static',express.static('public'));
Copy after login
Express.static can help us host static files, js, css, img, etc.

express.static use

#Basic use

There is css under public in the project directory , js, img and other folders.

I really need to host them through express so that we can access the data when we start the server.

Add

var express = require('express');
var app = express();
app.use(express.static('public'));
Copy after login
in app.js and we can pass

http://localhost:3000/css/style.css

http://localhost :3000/js/style.js
http://localhost:3000/img/style.png

Note:The paths of all files are relative to the storage directory. Therefore, the directory name where the static files are stored will not appear in the URL.

Virtual directory

This is achieved by specifying a mounting path for the static resource directory.

We can add a virtual directory to our static files, which sometimes makes it easier for us to manage our URLs in a unified manner, and also see the

attributes of the resources at a glance.

var express = require('express');
var app = express();
app.use('static',express.static('public'));
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to operate Vue to remove the # sign in the path

How to use vue to click on the blank space Hidden div implementation

The above is the detailed content of How to host static files with Express. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

In-depth analysis of the role and usage of the static keyword in C language In-depth analysis of the role and usage of the static keyword in C language Feb 20, 2024 pm 04:30 PM

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

In-depth comparison of Express and Laravel: How to choose the best framework? In-depth comparison of Express and Laravel: How to choose the best framework? Mar 09, 2024 pm 01:33 PM

In-depth comparison of Express and Laravel: How to choose the best framework? When choosing a back-end framework suitable for your project, Express and Laravel are undoubtedly two popular choices among developers. Express is a lightweight framework based on Node.js, while Laravel is a popular framework based on PHP. This article will provide an in-depth comparison of the advantages and disadvantages of these two frameworks and provide specific code examples to help developers choose the framework that best suits their needs. Performance and scalabilityExpr

Comparative analysis of Express and Laravel: Choose the framework that suits you better Comparative analysis of Express and Laravel: Choose the framework that suits you better Mar 10, 2024 pm 10:15 PM

Express and Laravel are two very popular web frameworks, representing the excellent frameworks of the two major development languages ​​of JavaScript and PHP respectively. This article will conduct a comparative analysis of these two frameworks to help developers choose a framework that is more suitable for their project needs. 1. Framework Introduction Express is a web application framework based on the Node.js platform. It provides a series of powerful functions and tools that enable developers to quickly build high-performance web applications. Express

The role and application scenarios of private static methods in PHP The role and application scenarios of private static methods in PHP Mar 23, 2024 am 10:18 AM

The role and application scenarios of private static methods in PHP In PHP programming, a private static method is a special method type. It can only be accessed within the class in which it is defined and cannot be directly called from the outside. Private static methods are usually used for the internal logic implementation of a class, providing a way to encapsulate and hide details. At the same time, they have the characteristics of static methods and can be called without instantiating the class object. The following will discuss the role and application scenarios of private static methods, and provide specific code examples. Function: encapsulate and hide implementation details: private static

How to use GitHub for code hosting in PHP development How to use GitHub for code hosting in PHP development Jun 27, 2023 pm 02:10 PM

In modern software development, version control and code hosting are very important links. As the world's largest open source community and code hosting platform, GitHub provides developers with powerful version control and collaboration tools. This article will introduce how to use GitHub for code hosting to help PHP developers better manage and share their code. To create a GitHub account, first open GitHub's official website https://github.com/ and click "Signup" in the upper right corner of the homepage.

Let's talk about how node+express operates cookies Let's talk about how node+express operates cookies Jun 22, 2022 am 10:01 AM

How does node+express operate cookies? The following article will introduce to you how to use node to operate cookies. I hope it will be helpful to you!

Quickly master the techniques and methods of static relative positioning Quickly master the techniques and methods of static relative positioning Jan 18, 2024 am 11:18 AM

Fast static relative positioning is a very important positioning method in web development. It allows an element to be slightly adjusted relative to its normal position while still maintaining its position in the document flow. In this article, I will introduce in detail the use of fast static relative positioning, as well as some common application scenarios. First, we need to understand the basic concepts of fast static relative positioning. In CSS, there are four ways to position elements: static positioning, relative positioning, absolute positioning and fixed positioning. Static positioning is the default positioning method. The position of the element is determined by the document.

See all articles