Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server_middleware not work #354

Open
5 tasks done
amanoooo opened this issue Jan 9, 2025 · 3 comments
Open
5 tasks done

server_middleware not work #354

amanoooo opened this issue Jan 9, 2025 · 3 comments

Comments

@amanoooo
Copy link

amanoooo commented Jan 9, 2025

Check List

  • I have already read Docs page & Troubleshooting page.
  • I have already searched existing issues and they are not help to me.
  • I examined error or warning messages and it's difficult to solve.
  • I am using the latest version of Hexo. (run hexo version to check)
  • My Node.js is matched the required version.

Expected behavior

I have a file filter1.js in scripts. '1111' works, but 3333 not work in request

hexo.extend.filter.register("server_middleware", function addHeader(app) {
    console.log('1111');
    
    app.use(function addHeaderHandler(req, res, next) {
      res.setHeader("X-222", "3333");
      next();
    }, 9999999);
  });

Actual behavior

still return X-powered-by = hexo in resposne headers

How to reproduce?

  1. npx hexo server

Is the problem still there under Safe mode?

no

Your Node.js & npm version

v20.11.1

10.2.4

Your Hexo and Plugin version

> npm ls --depth 0
Debugger attached.
[email protected] D:\amano\amanosblog
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Your package.json

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
  },
  "hexo": {
    "version": "7.3.0"
  },
  "dependencies": {
    "hexo": "^7.3.0",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-category": "^2.0.0",
    "hexo-generator-index": "^4.0.0",
    "hexo-generator-tag": "^2.0.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-marked": "^6.3.0",
    "hexo-renderer-stylus": "^3.0.1",
    "hexo-server": "^3.0.0",
    "hexo-theme-landscape": "^1.0.0"
  }
}

Your site's _config.yml (Optional)

# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site
title: BLOG
subtitle: ''
description: ''
keywords:
author: amano
language: zh
timezone: ''

# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: https://blog.util.online
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
  trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
  trailing_html: true # Set to false to remove trailing '.html' from permalinks

# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:

# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link:
  enable: true # Open external links in new tab
  field: site # Apply to the whole site
  exclude: ''
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
syntax_highlighter: highlight.js
highlight:
  line_number: true
  auto_detect: false
  tab_replace: ''
  wrap: true
  hljs: false
prismjs:
  preprocess: true
  line_number: true
  tab_replace: ''

# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
  path: ''
  per_page: 10
  order_by: -date

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Metadata elements
## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
meta_generator: true

# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
## updated_option supports 'mtime', 'date', 'empty'
updated_option: 'mtime'

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Include / Exclude file(s)
## include:/exclude: options only apply to the 'source/' folder
include:
exclude:
ignore:

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape

theme_config:
  # banner: /images/r2s.jpg
  # rss:
#  links:
#    github: https://github.com/amanoooo
#    author: https://util.online

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
  type: ''

Others

No response

@uiolee uiolee transferred this issue from hexojs/hexo Jan 9, 2025
@uiolee
Copy link
Member

uiolee commented Jan 9, 2025

server_middleware is not a predetermined filter of hexo, it is defined and executed by hexo-server

@uiolee
Copy link
Member

uiolee commented Jan 9, 2025

image

server_middleware could works. "hexo-server" has multiple server_middleware filters.

it doesn't work as you expected due to the order of filter execution or the order of connect execution.
One of solutions is changing the priority of filter.
(hexojs/hexo#1030 (comment))
https://hexo.io/api/filter.html#Synopsis

@amanoooo
Copy link
Author

It does work on 404 page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants