Node.Js - Get windows username

后端 未结 6 1599
臣服心动
臣服心动 2021-02-05 09:50

I am trying to get the windows username of the machine that is running my node.jS app (the app is always running on a Windows machine).

How can I get the current windows

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 09:57

    I believe you are stating that you have a Asp.NET application, and would like to use Node.js to determine the current users username, and then submit it to your Asp.NET application.

    I do not develop on Windows though from this question I believe this may be stored as an environment variable. process.env is a javascript map / dict of environment variables and likely contains the users username.

    Alternatively you can parse it from the users home directory as such :

    var path = require('path'); var username = path.sep(process.env['USERPROFILE'])[2];

    The question I linked above implies that USERPROFILE resolves to C:\Users\USERNAME\. I then split the path and take the 3rd element, being the username.

    Again, I do not have a windows machine and could not confirm this. Hope this sets you down the right path though.

    Cheers.

提交回复
热议问题