Rapid developments in the field of processors and radio technology have provided the context of small sensor nodes which are able to communicate in wireless sensor networks. But data security is restricted by these factors. An intelligent trust management plan needs to be applied for detection of the reliability of sensor nodes to distinguish between malicious nodes from other nodes. In this paper a trust-based energy-aware routing algorithm has been proposed. Considering direct and indirect trust of nodes and energy saving issue, the routing function metrics are optimized by Gravitational Search Approach. This routing method is called Energy-aware Trust-based Gravitational Search Approach (ETGSA). Simulation has been done by NS-2 and it is shown that the main characteristics such as reduction of computational overhead and increasing reliability against of attacks in the network are improved compared with other efficient algorithms in this field.