Node.js中SQLite Store默认会话文件路径修改方法问询
How to Change the Default Path for SQLiteStore in Express Session
Hey there! I see you're using SQLiteStore with express-session and want to move that default sessions file out of your root directory—totally makes sense for keeping your project organized. Here's how to fix it:
The Solution: Pass a Configuration Object to SQLiteStore
When initializing SQLiteStore, you can pass an options object to specify the exact path and filename for your session database. The key you need here is db:
const session = require('express-session'); const SQLiteStore = require('connect-sqlite3')(session); app.use(session({ store: new SQLiteStore({ db: './path/to/your/sessions.db' // Replace this with your custom path }), secret:"xxxxxx", saveUninitialized: false, cookie: { maxAge: 60000 }, rolling: true, resave: true }))
Quick Tips to Avoid Issues:
- Relative vs Absolute Paths: Use a relative path (like
./data/sessions.db) to place the file in a project subfolder, or an absolute path (e.g.,/home/user/my-app/sessions.dbon Linux/macOS, orC:\\my-app\\sessions.dbon Windows) for a specific system location. - Create Parent Folders First: If you're targeting a subfolder that doesn't exist yet (like
./datain the example), make sure to create that folder manually before running your app—SQLite can't create files in non-existent directories and will throw an error. - Just Rename the File? If you don't need to move the path, just want a different filename, simply use
db: 'my-custom-sessions.db'—this will create the file in your root directory with the new name.
That's all you need! Your session data will now live exactly where you want it, instead of cluttering up your project root.
内容的提问来源于stack exchange,提问作者Miqez
相关产品推荐
相关产品推荐

