Node.js data stored in the session is undefined issues (resolved)

Node.js data stored in the session is undefined issues (resolved) - two solutions

problem:

When the client via an interface data stored in the server session, the result of accessing session again through another interface, a print outundefined

analysis:

In the case of server code to ensure that no problem of access to data in less than a session, it must be the reason: the server to the client as two visits to two different clients.

Solution 1:

Front-end cross-domain must solve the problem!
Here I solutions from cross-domain issues vue-cli in:
configfile of index.jsthe proxyTableamended as follows:

proxyTable: {
      '/api': {
        target: 'http://localhost:3000',  // 接口的域名
        // secure: false,  // 如果是https接口,需要配置这个参数
        changeOrigin: true,	// 如果接口跨域,需要进行这个参数配置,为true的话,请求的header将会设置								为匹配目标服务器的规则(Access-Control-Allow-Origin)
        pathRewrite: { //本身的接口地址没有 '/api' 这种通用前缀,所以要rewrite,如果本身有则去掉 
          '^/api': ''
        }

After setting we request interface will be localhost:3000changed /api.

const BASE_URL='/api'	// 基础路径
// 请求数据
export const getHomeCasual = ()=> ajax(BASE_URL + '/api/homecasual');

Solution 2:

Server will localhostand 127.0.0.1recognized as two different clients!

The front end of the project when requesting data, respectively, if the use localhostand 127.0.0.1access to two interfaces, then node.js server will request these two visits seen as two different clients.

Therefore, be sure to use a unified front-end project localhostor 127.0.0.1!

Released six original articles · won praise 6 · views 338

Guess you like

Origin blog.csdn.net/qq_42586895/article/details/104080637