res.locals和req.flash()的目的

时间:2019-07-13 04:20:32

标签: javascript node.js

我正在使用private static final int CAMERA_REQUEST = 1888; private ImageView master,slave; Bitmap photo,slave_photo; Mat contour_slave; private static final int MY_CAMERA_PERMISSION_CODE = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button photoButton1 = (Button) this.findViewById(R.id.capture); photoButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE); } else { Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); } } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == MY_CAMERA_PERMISSION_CODE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show(); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); } else { Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show(); } } } protected void onActivityResult ( int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { photo = (Bitmap) data.getExtras().get("data"); this.master = findViewById(R.id.master); master.setImageBitmap(photo); } } public void getContours(View view) { Toast.makeText(this, "Test-1", Toast.LENGTH_SHORT).show(); contour_slave=new Mat(); Utils.bitmapToMat(photo,contour_slave); Mat gray = new Mat(); Imgproc.cvtColor(contour_slave, gray, Imgproc.COLOR_RGBA2GRAY); Imgproc.Canny(gray, gray, 50, 200); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); // find contours: Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE); for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) { Imgproc.drawContours(contour_slave, contours, contourIdx, new Scalar(0, 0, 255), -1); } Utils.matToBitmap(gray,slave_photo); this.slave = findViewById(R.id.slave); slave.setImageBitmap(slave_photo); } npm软件包。对于用户自发化,我检查了来自表单的用户名或密码,并相应地填充了Flash消息。

示例:

connect-flash

在登录页面中,我使用:

req.flash('loginMessage', 'Incorrect Password')

req.flash('loginMessage', 'No username found')

但是,我在其他人的代码中已经看到很多:

router.get("/login", function (req, res) {
    //console.log(req.flash('error'))
    res.render("login", {
        message: req.flash('loginMessage')
    })
})

Express docs将res.locals定义为

  

一个对象,它包含范围为   要求,因此仅适用于在   该请求/响应周期(如果有)。否则,此属性为   与app.locals相同。

如果每次都与app.use(function(req,res,next){ res.locals.error=req.flash("error"); res.locals.success=req.flash("success"); next() }) 对象类似地删除locals。仅使用Flash与将其存储在locals变量之间有什么区别?

1 个答案:

答案 0 :(得分:0)

区别在于,locals在当前请求结束时被删除 ,flash在显示后被删除 ,并且中间存储在会话中。

这意味着,如果在请求结束时未呈现任何页面(例如,重定向,直接res.end()等),则您的locals将丢失并丢失,但是另一方面,flash仍将是可检索的,因为会话的使用在请求之间是持久的。