enRaiser enRaiser - 4 months ago 21
Node.js Question

How to replace object ID with alias in URL

I have just installed keystone.js and build basic site.
However I noticed that URL contains the mongoDB ID of length 24. that looks ugly.
Any Idea, how can I change it.

http://localhost:3000/keystone/users/56ed6816491debf405f99be1
http://localhost:3000/keystone/posts/56ed086c4b4ba4f8044bbbe1

I want it to be like
http://localhost:3000/keystone/users/enraiser
http://localhost:3000/keystone/posts/my-first-post

Answer

We can use auto key of keystones

var Post = new keystone.List('Post', {
    autokey: { path: 'slug', from: 'title', unique: true },map: { name: 'title' },
    defaultSort: '-createdAt'
});