How to host static files with Express
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'));
express.static 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. Addvar express = require('express'); var app = express(); app.use(express.static('public'));
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 theattributes of the resources at a glance.
var express = require('express'); var app = express(); app.use('static',express.static('public'));
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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? 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

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 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

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.

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!

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.
